//author:kakake
//1.预约挂号支付，调起收银台支付，支付成功后会回调通知tspaynotifyController
//2.预约挂号退费，调起退费接口，退款成功后会回调通知tspaynotifyController
//3.自助缴费支付，调起收银台支付，支付成功后会回调通知tspaynotifyController

//author:kakake
var Common = /** @class */ (function () {
    function Common() {
    }
    return Common;
}());

//author:kakake
var tshisinterfaceController = /** @class */ (function () {
    function tshisinterfaceController(workid) {
        this.data = {
            dbConn: TSLobsterAPI.DbHelper.setdbconn('81.68.197.71', 'MiniHIS_Stable', 'sa', 'Lobster@123'),
            WorkId: 1,
            PatTypeID: 100,
            ChargeEmpID: 1322,
            ChargeEmpName: '机器人',
            BalanceType: 1
        };
        this.data.WorkId = workid;
        this.dao = new tshisinterfaceDao(workid, this.data.dbConn, this.data.ChargeEmpID, this.data.ChargeEmpName);
        this.registerservice = new registerService(workid, this.data.dbConn, this.data.PatTypeID, this.data.ChargeEmpID, this.data.ChargeEmpName);
        this.cancelregisterservice = new cancelregisterService(workid, this.data.dbConn, this.data.PatTypeID, this.data.ChargeEmpID, this.data.ChargeEmpName);
        this.balanceservice = new balanceService(workid, this.data.dbConn, this.data.PatTypeID, this.data.ChargeEmpID, this.data.ChargeEmpName, this.data.BalanceType);
        this.reportresultservice = new reportResultService(workid, this.data.dbConn);
    }
    /**
     * 获取科室分类和科室
     * 返回格式：
     * {"layer":[{"LayerId":3,"PId":0,"Name":"全部","WorkId":1},{"LayerId":2077,"PId":3,"Name":"互联网门诊","WorkId":1}],"dept":[{"DeptId":1067,"Layer":3,"Name":"产科门诊","Pym":"ckmz","Wbm":"utuy","Szm":"                    ","Code":"1001","DelFlag":0,"SortOrder":0,"Memo":"","WorkId":1,"ID":70,"DeptID":1067,"WorkID":1,"DeptType":0,"Principal":"帅凌宏","Telephone":"13984357402","Member_Num":0,"Bed_Num":0,"OutUsed":1,"InUsed":0,"PrtWardUsed":0,"PharmacyUsed":0,"ExamineUsed":0,"DrugUsed":0,"MaterialsUsed":0,"DeptAddress":"门诊二楼","IsOperation":0},{"DeptId":1068,"Layer":3,"Name":"儿科门诊","Pym":"ekmz","Wbm":"qtuy","Szm":"                    ","Code":"1002","DelFlag":0,"SortOrder":0,"Memo":"","WorkId":1,"ID":71,"DeptID":1068,"WorkID":1,"DeptType":0,"Principal":"罗详丁","Telephone":"18111912196","Member_Num":0,"Bed_Num":0,"OutUsed":1,"InUsed":0,"PrtWardUsed":0,"PharmacyUsed":0,"ExamineUsed":0,"DrugUsed":0,"MaterialsUsed":0,"DeptAddress":"门诊二楼","IsOperation":0},{"DeptId":1069,"Layer":3,"Name":"妇科门诊","Pym":"jsmz","Wbm":"ytuy","Szm":"                    ","Code":"1003","DelFlag":0,"SortOrder":0,"Memo":"","WorkId":1,"ID":72,"DeptID":1069,"WorkID":1,"DeptType":0,"Principal":"孟晓静","Telephone":"13985552070","Member_Num":0,"Bed_Num":0,"OutUsed":1,"InUsed":0,"PrtWardUsed":0,"PharmacyUsed":0,"ExamineUsed":0,"DrugUsed":0,"MaterialsUsed":0,"DeptAddress":"门诊二楼","IsOperation":0},{"DeptId":1070,"Layer":3,"Name":"儿童保健部","Pym":"bjk","Wbm":"wwt","Szm":"                    ","Code":"1005","DelFlag":0,"SortOrder":0,"Memo":"","WorkId":1,"ID":73,"DeptID":1070,"WorkID":1,"DeptType":0,"Principal":"罗详丁","Telephone":"18111912196","Member_Num":0,"Bed_Num":0,"OutUsed":1,"InUsed":0,"PrtWardUsed":0,"PharmacyUsed":0,"ExamineUsed":0,"DrugUsed":0,"MaterialsUsed":0,"DeptAddress":"门诊二楼","IsOperation":0}]}
     *
     * */
    tshisinterfaceController.prototype.getdept = function () {
        return this.dao.getdept();
    };
    /**
     * 预约排班
     * 明细只有上午、下午、晚上
    [{
        "DeptId": 1067,
        "DoctId": 245,
        "GoodNum": 0,
        "RegNum": 0,
        "DeptName": "产科门诊",
        "DocName": "林葛",
        "scheduleStatus": 0,
        "ItemA": [{
            "RegId": 12093,
            "DeptId": 1067,
            "DoctId": 245,
            "DeptName": "产科门诊",
            "DocName": "林葛",
            "RegDate": "2022-12-08",
            "Week": 4,
            "RegFee": 0.01,
            "TimeType": 1,
            "AllCount": 10,
            "HaveCount": 10,
            "scheduleStatus": 1
        }, {
            "RegId": 12094,
            "DeptId": 1067,
            "DoctId": 245,
            "DeptName": "产科门诊",
            "DocName": "林葛",
            "RegDate": "2022-12-08",
            "Week": 4,
            "RegFee": 0.01,
            "TimeType": 2,
            "AllCount": 20,
            "HaveCount": 20,
            "scheduleStatus": 1
        }]
    }]
    */
    tshisinterfaceController.prototype.getdocOrderSched = function (_deptId, _empId) {
        return this.dao.getdocOrderSched(_deptId, _empId);
    };
    /**
     * 排班时间段，比如精确到分钟
     * 一个医生上午看50个病人，那么就可以建50个时段
     * 时段没有配置的话，默认上午、下午、晚上三个时段
     * [{
            "Timeid": 2,
            "RegId": 12098,
            "DoctId": 245,
            "DeptId": 1067,
            "DeptName": "产科门诊",
            "DocName": "林葛",
            "RegDate": "2022-12-18",
            "IsHave": 1,
            "HaveCount": 10,
            "AllCount": 10,
            "IsAll": 1,
            "Week": 7,
            "RegFee": 0.01,
            "TimeType": 2,
            "StartTime": "14:00",
            "EndTime": "18:00"
        }, {
            "Timeid": 3,
            "RegId": 12099,
            "DoctId": 245,
            "DeptId": 1067,
            "DeptName": "产科门诊",
            "DocName": "林葛",
            "RegDate": "2022-12-18",
            "IsHave": 1,
            "HaveCount": 5,
            "AllCount": 5,
            "IsAll": 1,
            "Week": 7,
            "RegFee": 0.01,
            "TimeType": 3,
            "StartTime": "18:00",
            "EndTime": "23:00"
        }, {
            "Timeid": 1,
            "RegId": 12102,
            "DoctId": 245,
            "DeptId": 1067,
            "DeptName": "产科门诊",
            "DocName": "林葛",
            "RegDate": "2022-12-13",
            "IsHave": 1,
            "HaveCount": 3,
            "AllCount": 5,
            "IsAll": 1,
            "Week": 2,
            "RegFee": 0.01,
            "TimeType": 1,
            "StartTime": "8:30",
            "EndTime": "12:00"
        }]
     */
    tshisinterfaceController.prototype.scheduleTimeRange = function (_regid) {
        return this.dao.scheduleTimeRange(_regid);
    };
    /**
     * 挂号
     * { result: true, Regno: 123456, Location: '' }
     */
    tshisinterfaceController.prototype.register = function (param) {
        return this.registerservice.hisregister(param.DeptId, param.DoctId, param.Regid, param.RegDate, param.Timeid, param.CardType, param.CardNum, param.TranSerNo, param.PayTime, param.PayMode, param.TotalRegFee);
    };
    //取消挂号
    tshisinterfaceController.prototype.cancelregister = function (param) {
        if (!param.Regno || param.Regno == "") {
            throw "参数：Regno不能为空";
        }
        return this.cancelregisterservice.cancelregister(param.Regno, param.RegDate);
    };
    /**
     * 待缴费处方，处方3天有效，只查询三天内的处方
     * [{"CardType":1,"CardNum":"100000","Regno":28485,"PatName":"测试","Repno":"148203_1","PatListId":28485,"PresHeadID":148203,"PresDoctorID":100,"PresDoctName":"超级用户","PresDeptID":1067,"PresDeptName":"产科门诊","ExecDeptId":1067,"ExecDeptName":"产科门诊","PresDate":"2022-12-13 15:30:16","FeeTime":"2022-12-13","ItemName":"普通门诊挂号费","FeeType":3,"FeeTypeName":3,"UnitPrice":0.01,"Quantity":1,"Specs":"","Usage":null,"Frequency":null,"Unit":"","InsurFee":0,"InsurTypeName":0,"InfsurType":0,"CanUseInsurance":0,"InsuAdminfo":0},{"CardType":1,"CardNum":"100000","Regno":28485,"PatName":"测试","Repno":"148205_16125","PatListId":28485,"PresHeadID":148205,"PresDoctorID":100,"PresDoctName":"超级用户","PresDeptID":1067,"PresDeptName":"产科门诊","ExecDeptId":1081,"ExecDeptName":"影像科","PresDate":"2022-12-13 16:47:45","FeeTime":"2022-12-13","ItemName":"腹部彩色多普勒超声常规检查","FeeType":4,"FeeTypeName":4,"UnitPrice":75,"Quantity":1,"Specs":"","Usage":null,"Frequency":null,"Unit":"","InsurFee":0,"InsurTypeName":0,"InfsurType":0,"CanUseInsurance":0,"InsuAdminfo":0},{"CardType":1,"CardNum":"100000","Regno":28485,"PatName":"测试","Repno":"148205_16125","PatListId":28485,"PresHeadID":148205,"PresDoctorID":100,"PresDoctName":"超级用户","PresDeptID":1067,"PresDeptName":"产科门诊","ExecDeptId":1081,"ExecDeptName":"影像科","PresDate":"2022-12-13 16:47:45","FeeTime":"2022-12-13","ItemName":"阴道B超","FeeType":4,"FeeTypeName":4,"UnitPrice":124.25,"Quantity":1,"Specs":"","Usage":null,"Frequency":null,"Unit":"","InsurFee":0,"InsurTypeName":0,"InfsurType":0,"CanUseInsurance":0,"InsuAdminfo":0},{"CardType":1,"CardNum":"100000","Regno":28485,"PatName":"测试","Repno":"148205_16125","PatListId":28485,"PresHeadID":148205,"PresDoctorID":100,"PresDoctName":"超级用户","PresDeptID":1067,"PresDeptName":"产科门诊","ExecDeptId":1081,"ExecDeptName":"影像科","PresDate":"2022-12-13 16:47:45","FeeTime":"2022-12-13","ItemName":"胃肠道彩色多普勒超声常规检查","FeeType":4,"FeeTypeName":4,"UnitPrice":60,"Quantity":1,"Specs":"","Usage":null,"Frequency":null,"Unit":"","InsurFee":0,"InsurTypeName":0,"InfsurType":0,"CanUseInsurance":0,"InsuAdminfo":0},{"CardType":1,"CardNum":"100000","Regno":28485,"PatName":"测试","Repno":"148205_16125","PatListId":28485,"PresHeadID":148205,"PresDoctorID":100,"PresDoctName":"超级用户","PresDeptID":1067,"PresDeptName":"产科门诊","ExecDeptId":1081,"ExecDeptName":"影像科","PresDate":"2022-12-13 16:47:45","FeeTime":"2022-12-13","ItemName":"泌尿系彩色多普勒超声常规检查","FeeType":4,"FeeTypeName":4,"UnitPrice":75,"Quantity":1,"Specs":"","Usage":null,"Frequency":null,"Unit":"","InsurFee":0,"InsurTypeName":0,"InfsurType":0,"CanUseInsurance":0,"InsuAdminfo":0},{"CardType":1,"CardNum":"100000","Regno":28485,"PatName":"测试","Repno":"148205_16125","PatListId":28485,"PresHeadID":148205,"PresDoctorID":100,"PresDoctName":"超级用户","PresDeptID":1067,"PresDeptName":"产科门诊","ExecDeptId":1081,"ExecDeptName":"影像科","PresDate":"2022-12-13 16:47:45","FeeTime":"2022-12-13","ItemName":"妇科彩色多普勒超声常规检查","FeeType":4,"FeeTypeName":4,"UnitPrice":60,"Quantity":1,"Specs":"","Usage":null,"Frequency":null,"Unit":"","InsurFee":0,"InsurTypeName":0,"InfsurType":0,"CanUseInsurance":0,"InsuAdminfo":0},{"CardType":1,"CardNum":"100000","Regno":28485,"PatName":"测试","Repno":"148205_16125","PatListId":28485,"PresHeadID":148205,"PresDoctorID":100,"PresDoctName":"超级用户","PresDeptID":1067,"PresDeptName":"产科门诊","ExecDeptId":1081,"ExecDeptName":"影像科","PresDate":"2022-12-13 16:47:45","FeeTime":"2022-12-13","ItemName":"浅表器官彩色多普勒超声检查","FeeType":4,"FeeTypeName":4,"UnitPrice":60,"Quantity":1,"Specs":"","Usage":null,"Frequency":null,"Unit":"","InsurFee":0,"InsurTypeName":0,"InfsurType":0,"CanUseInsurance":0,"InsuAdminfo":0},{"CardType":1,"CardNum":"100000","Regno":28485,"PatName":"测试","Repno":"148205_16125","PatListId":28485,"PresHeadID":148205,"PresDoctorID":100,"PresDoctName":"超级用户","PresDeptID":1067,"PresDeptName":"产科门诊","ExecDeptId":1081,"ExecDeptName":"影像科","PresDate":"2022-12-13 16:47:45","FeeTime":"2022-12-13","ItemName":"门静脉系彩色多普勒超声","FeeType":4,"FeeTypeName":4,"UnitPrice":60,"Quantity":1,"Specs":"","Usage":null,"Frequency":null,"Unit":"","InsurFee":0,"InsurTypeName":0,"InfsurType":0,"CanUseInsurance":0,"InsuAdminfo":0},{"CardType":1,"CardNum":"100000","Regno":28485,"PatName":"测试","Repno":"148205_16125","PatListId":28485,"PresHeadID":148205,"PresDoctorID":100,"PresDoctName":"超级用户","PresDeptID":1067,"PresDeptName":"产科门诊","ExecDeptId":1081,"ExecDeptName":"影像科","PresDate":"2022-12-13 16:47:45","FeeTime":"2022-12-13","ItemName":"双肾及肾血管彩色多普勒超声","FeeType":4,"FeeTypeName":4,"UnitPrice":65,"Quantity":1,"Specs":"","Usage":null,"Frequency":null,"Unit":"","InsurFee":0,"InsurTypeName":0,"InfsurType":0,"CanUseInsurance":0,"InsuAdminfo":0},{"CardType":1,"CardNum":"100000","Regno":28485,"PatName":"测试","Repno":"148205_16125","PatListId":28485,"PresHeadID":148205,"PresDoctorID":100,"PresDoctName":"超级用户","PresDeptID":1067,"PresDeptName":"产科门诊","ExecDeptId":1081,"ExecDeptName":"影像科","PresDate":"2022-12-13 16:47:45","FeeTime":"2022-12-13","ItemName":"腔内彩色多普勒超声检查","FeeType":4,"FeeTypeName":4,"UnitPrice":110,"Quantity":1,"Specs":"","Usage":null,"Frequency":null,"Unit":"","InsurFee":0,"InsurTypeName":0,"InfsurType":0,"CanUseInsurance":0,"InsuAdminfo":0},{"CardType":1,"CardNum":"100000","Regno":28485,"PatName":"测试","Repno":"148205_16125","PatListId":28485,"PresHeadID":148205,"PresDoctorID":100,"PresDoctName":"超级用户","PresDeptID":1067,"PresDeptName":"产科门诊","ExecDeptId":1081,"ExecDeptName":"影像科","PresDate":"2022-12-13 16:47:45","FeeTime":"2022-12-13","ItemName":"临床操作的彩色多普勒超声引导","FeeType":4,"FeeTypeName":4,"UnitPrice":100,"Quantity":1,"Specs":"","Usage":null,"Frequency":null,"Unit":"","InsurFee":0,"InsurTypeName":0,"InfsurType":0,"CanUseInsurance":0,"InsuAdminfo":0},{"CardType":1,"CardNum":"100000","Regno":28484,"PatName":"测试","Repno":"148206_16126","PatListId":28484,"PresHeadID":148206,"PresDoctorID":100,"PresDoctName":"超级用户","PresDeptID":1067,"PresDeptName":"产科门诊","ExecDeptId":1081,"ExecDeptName":"影像科","PresDate":"2022-12-13 22:08:20","FeeTime":"2022-12-13","ItemName":"腹部彩色多普勒超声常规检查","FeeType":4,"FeeTypeName":4,"UnitPrice":75,"Quantity":1,"Specs":"","Usage":null,"Frequency":null,"Unit":"","InsurFee":0,"InsurTypeName":0,"InfsurType":0,"CanUseInsurance":0,"InsuAdminfo":0}]
     */
    tshisinterfaceController.prototype.getPatPresUnPay = function (cardType, cardNum) {
        return this.balanceservice.getPatPresUnPay(cardType, cardNum);
    };
    /**
     * 获取处方明细
     * {"Items":[{"PatName":"测试","CardNum":"100000","Repno":"148197_1","PresDate":"2022-12-11","ItemName":"普通门诊挂号费","Specs":"","Usage":null,"Frequency":null,"Quantity":1,"Unit":"","UnitPrice":0.01,"Fee":0.01,"Regno":28482,"DoctName":"超级用户","ExecDeptName":"产科门诊","DeptName":"产科门诊","PresType":3}],"CardNum":"100000","PatName":"测试","Feeno":"148197_1","DoctName":"超级用户","DeptName":"产科门诊","ExecDeptName":"产科门诊","PresType":3,"PresDate":"2022-12-11","Fee":"0.01"}
     */
    tshisinterfaceController.prototype.getPatPresDetail = function (repno) {
        return this.balanceservice.getPresDetail(repno);
    };
    /**
     * 已缴费处方
     * {"Items":[{"Repno":"148197_1","DeptName":"产科门诊","DoctName":"超级用户","FeeTime":"2022-12-11","FeeTypeName":3,"PersonFee":0.01,"InsurFee":0,"InsurTypeName":0,"InfsurType":0,"CanUseInsurance":0,"InsuAdminfo":0,"ExecDeptID":1067,"ExecDeptName":"产科门诊","Regno":28482,"Memo":"普通门诊挂号费"},{"Repno":"148197_2","DeptName":"产科门诊","DoctName":"超级用户","FeeTime":"2022-12-12","FeeTypeName":3,"PersonFee":31.86,"InsurFee":0,"InsurTypeName":0,"InfsurType":0,"CanUseInsurance":0,"InsuAdminfo":0,"ExecDeptID":1067,"ExecDeptName":"产科门诊","Regno":28482,"Memo":"艾条,长期留置针"},{"Repno":"148197_3","DeptName":"产科门诊","DoctName":"超级用户","FeeTime":"2022-12-12","FeeTypeName":3,"PersonFee":16.74,"InsurFee":0,"InsurTypeName":0,"InfsurType":0,"CanUseInsurance":0,"InsuAdminfo":0,"ExecDeptID":1067,"ExecDeptName":"产科门诊","Regno":28482,"Memo":"艾条"},{"Repno":"148197_4","DeptName":"产科门诊","DoctName":"超级用户","FeeTime":"2022-12-12","FeeTypeName":3,"PersonFee":16.74,"InsurFee":0,"InsurTypeName":0,"InfsurType":0,"CanUseInsurance":0,"InsuAdminfo":0,"ExecDeptID":1067,"ExecDeptName":"产科门诊","Regno":28482,"Memo":"艾条"},{"Repno":"148198_1","DeptName":"产科门诊","DoctName":"超级用户","FeeTime":"2022-12-11","FeeTypeName":3,"PersonFee":0.01,"InsurFee":0,"InsurTypeName":0,"InfsurType":0,"CanUseInsurance":0,"InsuAdminfo":0,"ExecDeptID":1067,"ExecDeptName":"产科门诊","Regno":28483,"Memo":"普通门诊挂号费"},{"Repno":"148198_2","DeptName":"产科门诊","DoctName":"超级用户","FeeTime":"2022-12-11","FeeTypeName":3,"PersonFee":7.2,"InsurFee":0,"InsurTypeName":0,"InfsurType":0,"CanUseInsurance":0,"InsuAdminfo":0,"ExecDeptID":1067,"ExecDeptName":"产科门诊","Regno":28483,"Memo":"小换药"},{"Repno":"148198_3","DeptName":"产科门诊","DoctName":"超级用户","FeeTime":"2022-12-11","FeeTypeName":3,"PersonFee":70.85,"InsurFee":0,"InsurTypeName":0,"InfsurType":0,"CanUseInsurance":0,"InsuAdminfo":0,"ExecDeptID":1067,"ExecDeptName":"产科门诊","Regno":28483,"Memo":"臭氧连接头,真空采血管（红头）"},{"Repno":"148198_4","DeptName":"产科门诊","DoctName":"超级用户","FeeTime":"2022-12-11","FeeTypeName":3,"PersonFee":211.38,"InsurFee":0,"InsurTypeName":0,"InfsurType":0,"CanUseInsurance":0,"InsuAdminfo":0,"ExecDeptID":1067,"ExecDeptName":"产科门诊","Regno":28483,"Memo":"臭氧连接头,真空采血管（红头）,真空采血管（绿头）,真空采血管（蓝头）,血凝杯"},{"Repno":"148199_1","DeptName":"产科门诊","DoctName":"超级用户","FeeTime":"2022-12-11","FeeTypeName":2,"PersonFee":20,"InsurFee":0,"InsurTypeName":0,"InfsurType":0,"CanUseInsurance":0,"InsuAdminfo":0,"ExecDeptID":2109,"ExecDeptName":"中药房","Regno":28483,"Memo":"败酱草"},{"Repno":"148200_16123","DeptName":"产科门诊","DoctName":"超级用户","FeeTime":"2022-12-11","FeeTypeName":4,"PersonFee":164.25,"InsurFee":0,"InsurTypeName":0,"InfsurType":0,"CanUseInsurance":0,"InsuAdminfo":0,"ExecDeptID":1081,"ExecDeptName":"影像科","Regno":28483,"Memo":"腹部彩色多普勒超声常规检查,引产术前B超"},{"Repno":"148201_16124","DeptName":"产科门诊","DoctName":"超级用户","FeeTime":"2022-12-11","FeeTypeName":4,"PersonFee":31.5,"InsurFee":0,"InsurTypeName":0,"InfsurType":0,"CanUseInsurance":0,"InsuAdminfo":0,"ExecDeptID":1080,"ExecDeptName":"检验科","Regno":28483,"Memo":"血常规五分类"},{"Repno":"148202_1","DeptName":"产科门诊","DoctName":"超级用户","FeeTime":"2022-12-12","FeeTypeName":1,"PersonFee":4.29,"InsurFee":0,"InsurTypeName":0,"InfsurType":0,"CanUseInsurance":0,"InsuAdminfo":0,"ExecDeptID":1075,"ExecDeptName":"西药房","Regno":28482,"Memo":"阿莫西林胶囊"}]}
     */
    tshisinterfaceController.prototype.getPatPresFinished = function (cardType, cardNum, startDate, endDate) {
        return this.balanceservice.getPatPresFinished(cardType, cardNum, startDate, endDate);
    };
    //缴费处方
    tshisinterfaceController.prototype.balancePatPresDetail = function (param) {
        return this.balanceservice.balance(param.Repno, param.TranserNo, param.PayTime, param.PayMode, param.payAmt);
    };
    /**
     * 获取检验结果
     * {"Items":[{"ApplyDetailID":151882,"ExamItemID":500291,"ExamItemName":"血常规五分类","ApplyNo":149447,"ApplyTime":"2022-12-11 16:10:52","ApplyDoc":"超级用户","ApplyDept":"产科门诊","InspectionTime":"2022-12-11 16:10:52","PatListID":28483,"CardNO":"100000","ApplyStatus":1,"Results":[{"ExamItemID":500291,"ExamItemName":"血常规五分类","ItemName":"凝血酶原时间","RefValue":"9—14","Value":"2","Unit":"S","Status":"↓","Dangerous":0,"CheckTime":"2017-09-06 15:50:57","CheckDoc":"管理员"},{"ExamItemID":500291,"ExamItemName":"血常规五分类","ItemName":"活化部分凝血酶时间","RefValue":"24.0—38","Value":"33","Unit":"S","Status":"","Dangerous":0,"CheckTime":"2017-09-06 15:50:57","CheckDoc":"管理员"},{"ExamItemID":500291,"ExamItemName":"血常规五分类","ItemName":"凝血酶时间","RefValue":"9.0—16.0","Value":"1","Unit":"S","Status":"↓","Dangerous":0,"CheckTime":"2017-09-06 15:50:57","CheckDoc":"管理员"},{"ExamItemID":500291,"ExamItemName":"血常规五分类","ItemName":"纤维蛋白原","RefValue":"2—4","Value":"331","Unit":"g/L","Status":"↑","Dangerous":0,"CheckTime":"2017-09-06 15:50:57","CheckDoc":"管理员"},{"ExamItemID":500291,"ExamItemName":"血常规五分类","ItemName":"国际标准化比值(PTINR)","RefValue":"0.76—1.15","Value":"33","Unit":"","Status":"↑","Dangerous":0,"CheckTime":"2017-09-06 15:50:57","CheckDoc":"管理员"},{"ExamItemID":500291,"ExamItemName":"血常规五分类","ItemName":"D-二聚体","RefValue":"<1.0","Value":"22","Unit":"ug/ml","Status":"↑","Dangerous":0,"CheckTime":"2017-09-06 15:50:57","CheckDoc":"管理员"},{"ExamItemID":500291,"ExamItemName":"血常规五分类","ItemName":"活动度","RefValue":"80.00—150.00","Value":"1","Unit":"%","Status":"↓","Dangerous":0,"CheckTime":"2017-09-06 15:50:57","CheckDoc":"管理员"},{"ExamItemID":500291,"ExamItemName":"血常规五分类","ItemName":"PTR","RefValue":"0.82—1.15","Value":"44","Unit":"","Status":"↑","Dangerous":0,"CheckTime":"2017-09-06 15:50:57","CheckDoc":"管理员"}]}]}
     */
    tshisinterfaceController.prototype.getLisResultByCardNum = function (cardType, cardNum, startDate, endDate) {
        return this.reportresultservice.getLisResultByCardNum(cardType, cardNum, startDate, endDate);
    };
    /**
     * 获取检查结果
     * {"Items":[{"ApplyDetailID":151902,"ExamItemID":500265,"ExamItemName":"腹部彩色多普勒超声常规检查","ApplyNo":149450,"ApplyTime":"2022-12-13 22:08:20","ApplyDoc":"超级用户","ApplyDept":"产科门诊","InspectionTime":"2022-12-13 22:08:20","PatListID":28484,"CardNO":"100000","ApplyStatus":0,"Results":[]},{"ApplyDetailID":151892,"ExamItemID":500265,"ExamItemName":"腹部彩色多普勒超声常规检查","ApplyNo":149449,"ApplyTime":"2022-12-13 16:47:45","ApplyDoc":"超级用户","ApplyDept":"产科门诊","InspectionTime":"2022-12-13 16:47:45","PatListID":28485,"CardNO":"100000","ApplyStatus":0,"Results":[]},{"ApplyDetailID":151893,"ExamItemID":500267,"ExamItemName":"阴道B超","ApplyNo":149449,"ApplyTime":"2022-12-13 16:47:45","ApplyDoc":"超级用户","ApplyDept":"产科门诊","InspectionTime":"2022-12-13 16:47:45","PatListID":28485,"CardNO":"100000","ApplyStatus":0,"Results":[]},{"ApplyDetailID":151894,"ExamItemID":500269,"ExamItemName":"胃肠道彩色多普勒超声常规检查","ApplyNo":149449,"ApplyTime":"2022-12-13 16:47:45","ApplyDoc":"超级用户","ApplyDept":"产科门诊","InspectionTime":"2022-12-13 16:47:45","PatListID":28485,"CardNO":"100000","ApplyStatus":0,"Results":[]},{"ApplyDetailID":151895,"ExamItemID":500270,"ExamItemName":"泌尿系彩色多普勒超声常规检查","ApplyNo":149449,"ApplyTime":"2022-12-13 16:47:45","ApplyDoc":"超级用户","ApplyDept":"产科门诊","InspectionTime":"2022-12-13 16:47:45","PatListID":28485,"CardNO":"100000","ApplyStatus":0,"Results":[]},{"ApplyDetailID":151896,"ExamItemID":500271,"ExamItemName":"妇科彩色多普勒超声常规检查","ApplyNo":149449,"ApplyTime":"2022-12-13 16:47:45","ApplyDoc":"超级用户","ApplyDept":"产科门诊","InspectionTime":"2022-12-13 16:47:45","PatListID":28485,"CardNO":"100000","ApplyStatus":0,"Results":[]},{"ApplyDetailID":151897,"ExamItemID":500272,"ExamItemName":"浅表器官彩色多普勒超声检查","ApplyNo":149449,"ApplyTime":"2022-12-13 16:47:45","ApplyDoc":"超级用户","ApplyDept":"产科门诊","InspectionTime":"2022-12-13 16:47:45","PatListID":28485,"CardNO":"100000","ApplyStatus":0,"Results":[]},{"ApplyDetailID":151898,"ExamItemID":500273,"ExamItemName":"门静脉系彩色多普勒超声","ApplyNo":149449,"ApplyTime":"2022-12-13 16:47:45","ApplyDoc":"超级用户","ApplyDept":"产科门诊","InspectionTime":"2022-12-13 16:47:45","PatListID":28485,"CardNO":"100000","ApplyStatus":0,"Results":[]},{"ApplyDetailID":151899,"ExamItemID":500274,"ExamItemName":"双肾及肾血管彩色多普勒超声","ApplyNo":149449,"ApplyTime":"2022-12-13 16:47:45","ApplyDoc":"超级用户","ApplyDept":"产科门诊","InspectionTime":"2022-12-13 16:47:45","PatListID":28485,"CardNO":"100000","ApplyStatus":0,"Results":[]},{"ApplyDetailID":151900,"ExamItemID":500275,"ExamItemName":"腔内彩色多普勒超声检查","ApplyNo":149449,"ApplyTime":"2022-12-13 16:47:45","ApplyDoc":"超级用户","ApplyDept":"产科门诊","InspectionTime":"2022-12-13 16:47:45","PatListID":28485,"CardNO":"100000","ApplyStatus":0,"Results":[]},{"ApplyDetailID":151901,"ExamItemID":500276,"ExamItemName":"临床操作的彩色多普勒超声引导","ApplyNo":149449,"ApplyTime":"2022-12-13 16:47:45","ApplyDoc":"超级用户","ApplyDept":"产科门诊","InspectionTime":"2022-12-13 16:47:45","PatListID":28485,"CardNO":"100000","ApplyStatus":0,"Results":[]},{"ApplyDetailID":151880,"ExamItemID":500265,"ExamItemName":"腹部彩色多普勒超声常规检查","ApplyNo":149446,"ApplyTime":"2022-12-11 16:10:47","ApplyDoc":"超级用户","ApplyDept":"产科门诊","InspectionTime":"2022-12-11 16:10:47","PatListID":28483,"CardNO":"100000","ApplyStatus":1,"Results":[]},{"ApplyDetailID":151881,"ExamItemID":500286,"ExamItemName":"引产术前B超","ApplyNo":149446,"ApplyTime":"2022-12-11 16:10:47","ApplyDoc":"超级用户","ApplyDept":"产科门诊","InspectionTime":"2022-12-11 16:10:47","PatListID":28483,"CardNO":"100000","ApplyStatus":1,"Results":[]}]}
     */
    tshisinterfaceController.prototype.getPacsResultByCardNum = function (cardType, cardNum, startDate, endDate) {
        return this.reportresultservice.getPacsResultByCardNum(cardType, cardNum, startDate, endDate);
    };
    return tshisinterfaceController;
}());

//author:kakake
var tshisinterfaceDao = /** @class */ (function () {
    function tshisinterfaceDao(workid, dbconn, chargeEmpID, chargeEmpName) {
        this.data = {
            dbConn: null,
            WorkId: 1,
            ChargeEmpID: 1322,
            ChargeEmpName: '机器人'
        };
        this.data.WorkId = workid;
        this.data.dbConn = dbconn;
        this.data.ChargeEmpID = chargeEmpID;
        this.data.ChargeEmpName = chargeEmpName;
    }
    //获取科室分类和科室
    tshisinterfaceDao.prototype.getdept = function () {
        var strsql = "SELECT *  FROM BaseDeptLayer where workid=@workid ";
        var strsql2 = "SELECT *  FROM BaseDept a left join BaseDeptDetails b on a.DeptId=b.DeptID  where a.DelFlag = 0 and b.OutUsed=1 and a.workid=@workid ";
        var layer = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, { workid: this.data.WorkId });
        var dept = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql2, { workid: this.data.WorkId });
        return { layer: layer, dept: dept };
    };
    //预约排班
    //显示医生以及医生各个时段的号源
    tshisinterfaceDao.prototype.getdocOrderSched = function (_deptId, _empId) {
        var _this = this;
        var deptId = _deptId || 0;
        var empId = _empId || 0;
        var strsql = "SELECT DeptID DeptId,DocEmpID DoctId,0 GoodNum,0 RegNum,\
                            dbo.fnGetDeptName(DeptID) DeptName,dbo.fnGetEmpName(DocEmpID) DocName FROM OP_DocSchedual\
                            where (DeptID=@deptId or 0>=@deptId) and (DocEmpID=@empId or 0>=@empId) AND\
                            SchedualDate>=CONVERT(varchar(10), GETDATE(), 120) AND\
                            SchedualDate<=CONVERT(varchar(10), DATEADD(DAY,6,GETDATE()),120)\
                            GROUP BY DeptID,DocEmpID";
        var dbparams = {
            deptId: deptId,
            empId: empId
        };
        var Items = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams);
        Items.forEach(function (i) {
            strsql = "SELECT SchedualID RegId\
           ,DeptID DeptId,DocEmpID DoctId,dbo.fnGetDeptName(DeptID) DeptName,dbo.fnGetEmpName(DocEmpID) DocName \
           ,CONVERT(varchar(10), SchedualDate, 120) RegDate \
           ,Datepart(weekday, SchedualDate + @@DateFirst - 1) Week \
           ,0.01 RegFee \
           ,SchedualTimeRange TimeType \
           ,AppointNum AllCount \
           ,AppointNum-(SELECT COUNT(1) FROM OP_PatList where RegTimeRange=OP_DocSchedual.SchedualTimeRange and RegDeptID=OP_DocSchedual.DeptID and RegEmpID=OP_DocSchedual.DocEmpID and CONVERT(varchar(10), RegDate, 120)=CONVERT(varchar(10), OP_DocSchedual.SchedualDate, 120) and IsYy=1) HaveCount \
           ,1 scheduleStatus \
           FROM OP_DocSchedual where DeptID=@DeptId and DocEmpID=@DoctId AND \
           SchedualDate>=CONVERT(varchar(10), GETDATE(), 120) AND SchedualDate<=CONVERT(varchar(10), DATEADD(DAY,6,GETDATE()),120)";
            var dbparams2 = {
                DeptId: i.DeptId,
                DoctId: i.DoctId
            };
            i.scheduleStatus = 0;
            i.ItemA = TSLobsterAPI.DbHelper.DbGetJsonData(_this.data.dbConn, strsql, dbparams2);
        });
        return Items;
    };
    //排班时间段
    tshisinterfaceDao.prototype.scheduleTimeRange = function (_regid) {
        //1.查询指定号源的时段
        var strsql = "SELECT TOP 1 \
             SchedualTimeRange Timeid \
             ,case SchedualTimeRange WHEN 1 then '8:30' when 2 then '14:00'  else '18:00' end StartTime \
             ,case SchedualTimeRange WHEN 1 then '12:00' when 2 then '18:00' else '23:00' end EndTime \
             ,AppointNum AllCount \
             ,AppointNum-(SELECT COUNT(1) FROM OP_PatList where RegTimeRange=OP_DocSchedual.SchedualTimeRange and RegDeptID=DeptID and RegEmpID=DocEmpID and CONVERT(varchar(10), RegDate, 120)=CONVERT(varchar(10), SchedualDate, 120) and IsYy=1) HaveCount \
             FROM OP_DocSchedual where SchedualID=@regid";
        var dbparams = {
            regid: _regid
        };
        var Items = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams);
        return Items;
    };
    //得到当前结账ID,iAccountType:0挂号收费 1会员充值
    tshisinterfaceDao.prototype.add_regCost_GetAccountID = function () {
        var AccountID = 0;
        var LastDate;
        var OP_Account = {}; //门诊交款对象
        var CurrDate = TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss"); //当前日期
        var strsql = "SELECT AccountID,LastDate FROM OP_Account WHERE AccountEmpID=@OperatorID AND AccountType=0 and AccountFlag=0 and WorkID=@WorkId";
        var dbparams = {
            OperatorID: this.data.ChargeEmpID,
            WorkId: this.data.WorkId
        };
        var result = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        if (result) {
            AccountID = result["AccountID"];
        }
        else {
            //不存在未交款ID，一般都是第一笔
            strsql = "SELECT TOP 1 AccountID,LastDate FROM OP_Account WHERE AccountEmpID=@OperatorID AND AccountType=0 and AccountFlag=1 and WorkID=@WorkId \
                        ORDER BY AccountID DESC";
            var result2 = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
            if (result2) {
                LastDate = result["LastDate"];
            }
            else { //不存在上次缴费时间，这是这个收费员的第一笔
                LastDate = CurrDate;
            }
            OP_Account['AccountID'] = 0;
            OP_Account['LastDate'] = LastDate;
            OP_Account['TotalFee'] = 0;
            OP_Account['CashFee'] = 0;
            OP_Account['PromFee'] = 0;
            OP_Account['PosFee'] = 0;
            OP_Account['AccountEmpID'] = this.data.ChargeEmpID;
            OP_Account['AccountDate'] = CurrDate;
            OP_Account['AccountFlag'] = 0;
            OP_Account['ReceivFlag'] = 0;
            OP_Account['ReceivEmpID'] = 0;
            OP_Account['ReceivDate'] = "1900-01-01";
            OP_Account['ReceivBillNO'] = "";
            OP_Account['WorkID'] = this.data.WorkId;
            OP_Account['InvoiceCount'] = 0;
            OP_Account['RefundInvoiceCount'] = 0;
            OP_Account['RoundingFee'] = 0;
            OP_Account['AccountType'] = 0;
            OP_Account['AccountEmpName'] = this.data.ChargeEmpName;
            //插入OP_Account
            strsql = "INSERT INTO [OP_Account]([LastDate], [TotalFee], [CashFee], [PromFee], [PosFee], [AccountEmpID], [AccountDate] \
            , [AccountFlag], [ReceivFlag], [ReceivEmpID], [ReceivDate], [ReceivBillNO], [WorkID], [InvoiceCount], [RefundInvoiceCount] \
            , [RoundingFee], [AccountType], [AccountEmpName]) \
            VALUES (@LastDate, @TotalFee, @CashFee,@PromFee, @PosFee, @AccountEmpID, @AccountDate, \
            @AccountFlag, @ReceivFlag, @ReceivEmpID, @ReceivDate, @ReceivBillNO, @WorkID, @InvoiceCount, @RefundInvoiceCount, \
            @RoundingFee, @AccountType, @AccountEmpName); \
            SELECT @@IDENTITY;";
            AccountID = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, OP_Account);
        }
        return AccountID;
    };
    return tshisinterfaceDao;
}());

//author:kakake
var tshisinterfaceService = /** @class */ (function () {
    function tshisinterfaceService(workid, dbconn, patTypeID, chargeEmpID, chargeEmpName) {
        this.data = {
            dbConn: null,
            WorkId: 1,
            PatTypeID: 100,
            ChargeEmpID: 1322,
            ChargeEmpName: '机器人'
        };
        this.data.WorkId = workid;
        this.data.dbConn = dbconn;
        this.data.PatTypeID = patTypeID;
        this.data.ChargeEmpID = chargeEmpID;
        this.data.ChargeEmpName = chargeEmpName;
        this.dao = new tshisinterfaceDao(workid, dbconn, chargeEmpID, chargeEmpName);
    }
    tshisinterfaceService.prototype.getDeptName = function (DeptId) {
        var strsql = "select dbo.fnGetDeptName(@DeptId) RegDeptName";
        var dbparams = {
            DeptId: DeptId
        };
        var result = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        return result.RegDeptName;
    };
    tshisinterfaceService.prototype.getEmpName = function (DoctId) {
        var strsql = "select dbo.fnGetEmpName(@DoctId) RegDocName";
        var dbparams = {
            DoctId: DoctId
        };
        var result = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        return result.RegDocName;
    };
    tshisinterfaceService.prototype.getDeptAddress = function (deptId) {
        //返回科室地址
        var strsql = "select top 1 DeptAddress from basedeptdetails where deptId=@deptId";
        var dbparams = {
            deptId: deptId
        };
        var res = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        return res.DeptAddress;
    };
    //校验今日挂号时段是否已过
    tshisinterfaceService.prototype.CheckTodayTime = function (Timeid) {
        var dateNow = TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss").substring(0, 10);
        var currtime = new Date();
        var endtime = new Date();
        if (Timeid == 1) { //上午班
            endtime = new Date(dateNow + " 12:00:00");
        }
        else if (Timeid == 2) { //下午班
            endtime = new Date(dateNow + " 18:00:00");
        }
        else { //晚上班
            endtime = new Date(dateNow + " 22:00:00");
        }
        if (currtime > endtime) {
            return false;
        }
        else
            return true;
    };
    //校验排班是否有效
    tshisinterfaceService.prototype.CheckSchedule = function (Regid, DeptId, DoctId) {
        var strsql = "SELECT COUNT(1) co FROM OP_DocSchedual where SchedualID=@Regid and DeptID=@DeptId and DocEmpID=@DoctId and Flag=1";
        var dbparams = {
            Regid: Regid,
            DeptId: DeptId,
            DoctId: DoctId
        };
        var result = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        if (parseInt(result.co) > 0) {
            return true;
        }
        else {
            return false;
        }
    };
    //校验号源是否满
    tshisinterfaceService.prototype.CheckFull = function (Regid) {
        var strsql = "SELECT TOP 1 \
                        AppointNum-(SELECT COUNT(1) FROM OP_PatList where RegDeptID=OP_DocSchedual.DeptID and RegEmpID=OP_DocSchedual.DocEmpID and CONVERT(varchar(10), RegDate, 120)=CONVERT(varchar(10), OP_DocSchedual.SchedualDate, 120) and IsYy=1) HaveCount \
                        FROM OP_DocSchedual where SchedualID=@Regid";
        var dbparams = {
            Regid: Regid
        };
        var result = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        if (parseInt(result.HaveCount) > 0) {
            return true;
        }
        else {
            return false;
        }
    };
    //校验病人是否有效
    tshisinterfaceService.prototype.CheckPatient = function (CardType, CardNum) {
        var strsql = "SELECT COUNT(1) co FROM \
                            ME_MemberInfo a  \
                            INNER JOIN ME_MemberAccount b on a.MemberID=b.MemberID \
                            where b.CardTypeID=@CardType and b.CardNO=@CardNum and a.UseFlag=1 and b.UseFlag=1";
        var dbparams = {
            CardType: CardType,
            CardNum: CardNum
        };
        var result = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        if (parseInt(result.co) > 0) {
            return true;
        }
        else {
            return false;
        }
    };
    /**
     新增门诊费用
    //OP_FeeItemHead组装费用头表
    //List<OP_FeeItemDetail> 组装费用明细
    @param {String} type,Regno,Repno - type 0 挂号收费 1处方收费;Regno挂号ID;Repno处方号
    @return {String} return 费用头表
      */
    tshisinterfaceService.prototype.add_regFee = function (type, Regno, Repno) {
        var CurrDate = TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss"); //当前日期
        //1.挂号根据挂号类型自动生成费用
        //2.处方根据处方明细生成费用
        //3.结算记录生成后需要反写ID
        var OP_FeeItemHead = {
            "FeeItemHeadID": 0,
            "CostHeadID": 0,
            "MemberID": 0,
            "PatListID": 0,
            "PatName": "",
            "PresType": "0",
            "PresEmpID": 0,
            "PresDeptID": 0,
            "ExecEmpID": 0,
            "ExecDeptID": 0,
            "ChargeEmpID": this.data.ChargeEmpID,
            "PresAmount": 1,
            "TotalFee": 0,
            "OldID": 0,
            "InvoiceNO": "",
            "ReciptNO": "",
            "ChargeStatus": 0,
            "ChargeFlag": 0,
            "DistributeFlag": 0,
            "ExecDate": "",
            "PresDate": "",
            "RoungingFee": 0,
            "DocPresHeadID": 0,
            "DiscountFee": 0,
            "FeeNo": 0,
            "ChargeDate": "",
            "WorkID": this.data.WorkId,
            "RegFlag": 1,
            "DocPresNO": "",
            "DosageAllotmentID": 0,
            "DosageMedicalStatus": 0,
            "DosageMedicalDate": "",
            "DosageMedicalDEmp": 0,
            "SendAllotmentID": 0
        }; //费用头表
        var OP_FeeItemDetail = {
            "PresDetailID": 0,
            "FeeItemHeadID": 0,
            "MemberID": 0,
            "PatListID": 0,
            "ItemID": 0,
            "ItemType": 0,
            "StatID": 0,
            "ItemName": "",
            "Spec": "",
            "PackUnit": "",
            "UnitNO": 1,
            "StockPrice": 0,
            "RetailPrice": 0,
            "Amount": 1,
            "PresAmount": 1,
            "TotalFee": 0,
            "ExamItemID": 0,
            "DocPresDetailID": 0,
            "WorkID": this.data.WorkId,
            "MiniUnit": "",
            "Memo": "",
            "DocPresDate": CurrDate //当前日期
        }; //费用明细表
        var RegTypeID = 0; //挂号类型ID
        var List_FeeItemDetail = []; //费用明细列表
        var strsql = "SELECT MemberID,PatListID,PatName,RegEmpID,RegDeptID,RegTypeID,RegDate \
                                FROM OP_PatList WHERE PatListID=@Regno";
        var dbparams = {
            Regno: Regno
        };
        var result = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        RegTypeID = result["RegTypeID"];
        OP_FeeItemHead['MemberID'] = result["MemberID"];
        OP_FeeItemHead['PatListID'] = result["PatListID"];
        OP_FeeItemHead['PatName'] = result["PatName"];
        OP_FeeItemHead['PresEmpID'] = result["RegEmpID"];
        OP_FeeItemHead['PresDeptID'] = result["RegDeptID"];
        OP_FeeItemHead['ExecDeptID'] = result["RegDeptID"];
        OP_FeeItemHead['ExecEmpID'] = result["RegEmpID"];
        OP_FeeItemHead['ExecDate'] = CurrDate;
        OP_FeeItemHead['PresDate'] = CurrDate;
        OP_FeeItemDetail['MemberID'] = OP_FeeItemHead['MemberID'];
        OP_FeeItemDetail['PatListID'] = OP_FeeItemHead['PatListID'];
        OP_FeeItemDetail['WorkID'] = OP_FeeItemHead['WorkID'];
        if (type == 0) { //挂号
            OP_FeeItemHead['RegFlag'] = 1;
            //1.通过RegTypeID查询挂号费用明细
            OP_FeeItemHead['TotalFee'] = this.add_regFee_GetRegTypeFee(RegTypeID); //挂号费
            //获取费用明细表数据
            var strsql2 = "SELECT a.ItemID,b.ItemClass ItemType,b.StatID StatID,b.ItemName ItemName,b.Standard Spec \
            ,b.UnPickUnit PackUnit,b.MiniConvertNum UnitNO,b.InPrice StockPrice,b.SellPrice RetailPrice, b.SellPrice TotalFee \
            ,b.MiniUnitName MiniUnit FROM OP_RegItemFee a \
            LEFT JOIN ViewFeeItem_SimpleList b on a.ItemID=b.ItemID and a.WorkID=b.WorkID \
            WHERE a.RegTypeID=@RegTypeID";
            var dbparams2 = {
                RegTypeID: RegTypeID
            };
            var result2 = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql2, dbparams2) || [];
            result2.forEach(function (item) {
                var FeeItemDetail = JSON.parse(JSON.stringify(OP_FeeItemDetail)); //拷贝费用明细对象
                FeeItemDetail['ItemID'] = item["ItemID"];
                FeeItemDetail['ItemType'] = item["ItemType"];
                FeeItemDetail['StatID'] = item["StatID"];
                FeeItemDetail['ItemName'] = item["ItemName"];
                FeeItemDetail['Spec'] = item["Spec"];
                FeeItemDetail['PackUnit'] = item["PackUnit"];
                FeeItemDetail['UnitNO'] = item["UnitNO"];
                FeeItemDetail['StockPrice'] = item["StockPrice"];
                FeeItemDetail['RetailPrice'] = item["RetailPrice"];
                FeeItemDetail['TotalFee'] = item["TotalFee"];
                FeeItemDetail['MiniUnit'] = item["MiniUnit"];
                List_FeeItemDetail.push(FeeItemDetail);
            });
        }
        else if (type == 1) { //收费
            OP_FeeItemHead['RegFlag'] = 0;
            OP_FeeItemHead['PresAmount'] = 1; //?
            OP_FeeItemHead['TotalFee'] = this.add_regFee_GetPresFee(Repno); //?
            //TrackLog("zhyy0504_add_regFee", 52, Repno);
            //TrackLog("zhyy0504_add_regFee", 52, JSON.stringify(OP_FeeItemHead));
            OP_FeeItemHead['RoungingFee'] = 0; //?
            //获取费用头表数据
            var strsql2 = "SELECT TOP 1 t.Repno FeeNo,t.PresHeadID DocPresHeadID,t.PresNo DocPresNO \
                            , case t.StatID when 100 then '1' when 101 then '2' when 102 then '3' else 0 end PresType \
                            ,t.PresDate PresDate,t.PresDoctorID PresEmpID,t.PresDeptID PresDeptID, t.ExecDeptID,GETDATE() ExecDate, t.PresDoctorID ExecEmpID \
                            FROM ( \
                            select \
                            (CONVERT(VARCHAR(50),h.PresHeadID)+'_'+CONVERT(VARCHAR(20),d.PresNO)) Repno, \
                            d.PresDate,d.PresDoctorID,d.PresDeptID,d.ExecDeptID,d.StatID,d.PresHeadID,d.PresNO \
                            from OPD_PresDetail d \
                            left join OPD_PresHead  h on d.PresHeadID = h.PresHeadID \
                            ) t where t.Repno = @Repno";
            var dbparams2 = {
                Repno: Repno
            };
            var result2 = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql2, dbparams2);
            OP_FeeItemHead['FeeNo'] = 0; //result.getString("FeeNo");//?
            OP_FeeItemHead['DocPresHeadID'] = result2["DocPresHeadID"]; //?
            OP_FeeItemHead['DocPresNO'] = result2["DocPresNO"]; //?
            OP_FeeItemHead['PresType'] = result2["PresType"]; //?
            OP_FeeItemHead['PresDate'] = result2["PresDate"]; //当前日期?
            OP_FeeItemHead['PresEmpID'] = result2["PresEmpID"];
            OP_FeeItemHead['PresDeptID'] = result2["PresDeptID"];
            OP_FeeItemHead['ExecDate'] = result2["ExecDate"];
            OP_FeeItemHead['ExecDeptID'] = result2["ExecDeptID"];
            OP_FeeItemHead['ExecEmpID'] = result2["ExecEmpID"];
            //获取费用明细表数据
            var strsql3 = "SELECT * FROM ( \
                    select  \
                    (CONVERT(VARCHAR(50),h.PresHeadID)+'_'+CONVERT(VARCHAR(20),d.PresNO)) Repno \
                    ,d.ItemID \
                    ,f.ItemClass ItemType \
                    ,d.StatID \
                    ,d.ItemName \
                    ,d.Spec \
                    ,f.UnPickUnit PackUnit \
                    ,f.MiniConvertNum UnitNO \
                    ,f.InPrice StockPrice \
                    ,f.SellPrice RetailPrice \
                    ,d.Price*d.ChargeAmount TotalFee \
                    ,f.MiniUnitName MiniUnit \
                    ,d.ChargeAmount Amount \
                    ,CONVERT(int,d.PresAmount) PresAmount \
                    ,(case h.PresType when 4 then d.ItemID else 0 end) ExamItemID \
                    ,d.PresDetailID DocPresDetailID \
                    ,d.Memo \
                    from OPD_PresDetail d  \
                    left join OPD_PresHead  h on d.PresHeadID = h.PresHeadID \
                    LEFT JOIN ViewFeeItem_SimpleList f on d.ItemID=f.ItemID and d.WorkID=f.WorkID \
                    ) t where t.Repno =@Repno";
            var dbparams3 = {
                Repno: Repno
            };
            var result3 = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql3, dbparams3) || [];
            result3.forEach(function (item) {
                var FeeItemDetail = JSON.parse(JSON.stringify(OP_FeeItemDetail)); //拷贝费用明细对象
                FeeItemDetail['ItemID'] = item["ItemID"];
                FeeItemDetail['ItemType'] = item["ItemType"];
                FeeItemDetail['StatID'] = item["StatID"];
                FeeItemDetail['ItemName'] = item["ItemName"];
                FeeItemDetail['Spec'] = item["Spec"];
                FeeItemDetail['PackUnit'] = item["PackUnit"];
                FeeItemDetail['UnitNO'] = item["UnitNO"];
                FeeItemDetail['StockPrice'] = item["StockPrice"];
                FeeItemDetail['RetailPrice'] = item["RetailPrice"];
                FeeItemDetail['TotalFee'] = item["TotalFee"];
                FeeItemDetail['MiniUnit'] = item["MiniUnit"];
                FeeItemDetail['Amount'] = item["Amount"];
                FeeItemDetail['PresAmount'] = item["PresAmount"];
                FeeItemDetail['ExamItemID'] = item["ExamItemID"];
                FeeItemDetail['DocPresDetailID'] = item["DocPresDetailID"];
                FeeItemDetail['Memo'] = item["Memo"];
                List_FeeItemDetail.push(FeeItemDetail);
            });
        }
        this.add_regFee_SaveData(OP_FeeItemHead, List_FeeItemDetail); //保存费用数据
        return OP_FeeItemHead;
    };
    //根据挂号类型获取挂号费
    tshisinterfaceService.prototype.add_regFee_GetRegTypeFee = function (RegTypeID) {
        var strsql = "SELECT Amount FROM( \
        SELECT a.RegTypeID,a.DoctorPostCode,SUM(c.Price) Amount FROM OP_RegType a \
        LEFT JOIN OP_RegItemFee b on a.RegTypeID=b.RegTypeID and a.WorkID=b.WorkID \
        LEFT JOIN Basic_HospFeeItem c on b.ItemID=c.ItemID and b.WorkID=c.WorkID \
        WHERE b.Flag=0 AND a.WorkID=@WorkId \
        GROUP BY a.RegTypeID,a.DoctorPostCode) t where t.RegTypeID=@RegTypeID";
        var dbparams = {
            WorkId: this.data.WorkId,
            RegTypeID: RegTypeID
        };
        var result = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, dbparams);
        return result;
    };
    //获取处方的总费用
    tshisinterfaceService.prototype.add_regFee_GetPresFee = function (Repno) {
        var strsql = "SELECT SUM(Price*ChargeAmount) TotalFee FROM ( \
            select \
            (CONVERT(VARCHAR(50),h.PresHeadID)+'_'+CONVERT(VARCHAR(20),d.PresNO)) Repno, \
            d.Price,d.ChargeAmount \
            from OPD_PresDetail d \
            left join OPD_PresHead  h on d.PresHeadID = h.PresHeadID \
            ) t where t.Repno = @Repno ";
        var dbparams = {
            Repno: Repno
        };
        var result = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, dbparams);
        return result;
    };
    //保存费用数据
    tshisinterfaceService.prototype.add_regFee_SaveData = function (OP_FeeItemHead, List_FeeItemDetail) {
        //插入头表
        var strsql = "INSERT INTO [OP_FeeItemHead]([CostHeadID], [MemberID], [PatListID], [PatName], [PresType], [PresEmpID], [PresDeptID] \
        , [ExecEmpID], [ExecDeptID], [ChargeEmpID], [PresAmount], [TotalFee], [OldID], [InvoiceNO], [ReciptNO], [ChargeStatus] \
        , [ChargeFlag], [DistributeFlag], [ExecDate], [PresDate], [RoungingFee], [DocPresHeadID], [DiscountFee], [FeeNo], [ChargeDate] \
        , [WorkID], [RegFlag], [DocPresNO], [DosageAllotmentID], [DosageMedicalStatus], [DosageMedicalDate], [DosageMedicalDEmp], [SendAllotmentID]) \
        VALUES (@CostHeadID, @MemberID, @PatListID, @PatName, @PresType, @PresEmpID, @PresDeptID,\
        @ExecEmpID, @ExecDeptID, @ChargeEmpID, @PresAmount, @TotalFee, @OldID, @InvoiceNO,@ReciptNO,@ChargeStatus, \
        @ChargeFlag, @DistributeFlag, @ExecDate, @PresDate,@RoungingFee,@DocPresHeadID,@DiscountFee,@FeeNo, @ChargeDate, \
        @WorkID, @RegFlag, @DocPresNO, @DosageAllotmentID, @DosageMedicalStatus, @DosageMedicalDate, @DosageMedicalDEmp, @SendAllotmentID); \
        SELECT @@IDENTITY;";
        var result = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, OP_FeeItemHead);
        OP_FeeItemHead['FeeItemHeadID'] = parseInt(result);
        //插入到费用明细表
        for (var i = 0; i < List_FeeItemDetail.length; i++) {
            strsql = "INSERT INTO [OP_FeeItemDetail]([FeeItemHeadID], [MemberID], [PatListID], [ItemID], [ItemType], [StatID] \
                    , [ItemName], [Spec], [PackUnit], [UnitNO], [StockPrice], [RetailPrice], [Amount], [PresAmount], [TotalFee] \
                    , [ExamItemID], [DocPresDetailID], [WorkID], [MiniUnit], [Memo], [DocPresDate]) \
                    VALUES (@FeeItemHeadID, @MemberID, @PatListID, @ItemID, @ItemType, @StatID, \
                    @ItemName, @Spec,@PackUnit, @UnitNO, @StockPrice, @RetailPrice, @Amount, @PresAmount, @TotalFee, \
                    @ExamItemID, @DocPresDetailID, @WorkID, @MiniUnit, @Memo, @DocPresDate);";
            List_FeeItemDetail[i]['FeeItemHeadID'] = OP_FeeItemHead['FeeItemHeadID'];
            TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, List_FeeItemDetail[i]);
        }
    };
    /**
      新增门诊结算
      1、OP_CostHead\OP_CostDetail\OP_CostPayMentInfo
      2、回写费用表状态
      @param {String} type,Regno,feeHeadIDs,TranSerNo,PayTime,PayMode,TotalFee - 0 挂号收费 1处方收费;挂号ID,费用头表ID数组,交易流水号,支付时间,支付方式1微信2支付宝3其他,支付金额
      @return {String} return 结算对象
    */
    tshisinterfaceService.prototype.add_regCost = function (type, Regno, feeHeadIDs, TranSerNo, PayTime, PayMode, TotalFee) {
        //结算头表
        var OP_CostHead = {
            "CostHeadID": 0,
            "MemberID": 0,
            "MemberAccountID": 0,
            "CardNO": "",
            "PatListID": 0,
            "PatName": "",
            "PatTypeID": 0,
            "BeInvoiceNO": "",
            "EndInvoiceNO": "",
            "ChargeEmpID": this.data.ChargeEmpID,
            "ChargeEmpName": "",
            "TotalFee": 0,
            "CashFee": 0,
            "PosFee": 0,
            "PromFee": 0,
            "RecipeFlag": 0,
            "CostDate": null,
            "CostStatus": 0,
            "OldID": 0,
            "AccountID": 0,
            "RegFlag": 0,
            "RegCategory": 0,
            "RoundingFee": 0,
            "InvoiceID": 0,
            "WorkID": this.data.WorkId
        };
        //结算明细
        var OP_CostDetail = {
            "CostDetailID": 0,
            "CostHeadID": 0,
            "StatID": 0,
            "TotalFee": 0,
            "FeeItemHeadID": 0,
            "PresEmpID": 0,
            "PresDeptID": 0,
            "ExeDeptID": 0,
            "WorkID": this.data.WorkId
        };
        //结算支付方式
        var OP_CostPayMentInfo = {
            "ID": 0,
            "CostHeadID": 0,
            "PayMentID": 0,
            "PayMentName": "",
            "PayMentMoney": 0,
            "AccountID": 0,
            "PatType": 0,
            "PatListID": 0,
            "PatName": "",
            "WorkID": this.data.WorkId,
            "PayMentCode": "",
            "TradeOrderNo": "",
            "qlcOrderId": ""
        };
        var List_CostDetail = []; //结算明细表列表
        var List_CostPayMentInfo = []; //结算支付方式列表
        //填充数据，根据Regno, feeHeadIDs-----------------------------------
        //填充OP_CostHead
        this.add_regCost_GetOPCostHead(Regno, OP_CostHead);
        if (type == 0) { //挂号
            OP_CostHead['RegFlag'] = 1; //挂号
        }
        else if (type == 1) {
            OP_CostHead['RegFlag'] = 0; //收费
        }
        //获取发票号
        var invoicedata = this.add_regCost_GetInvoice();
        OP_CostHead["InvoiceID"] = invoicedata["ID"];
        OP_CostHead["BeInvoiceNO"] = invoicedata["PerfChar"] + invoicedata["CurrentNO"];
        OP_CostHead["EndInvoiceNO"] = invoicedata["PerfChar"] + invoicedata["CurrentNO"];
        OP_CostHead["TotalFee"] = this.add_regCost_TotalFee(feeHeadIDs); //总金额
        OP_CostHead["CashFee"] = 0; //现金
        OP_CostHead["PosFee"] = 0; //POS
        OP_CostHead["PromFee"] = 0; //优惠
        OP_CostHead["RoundingFee"] = 0; //凑整
        //根据收费人员ID,得到当前结账ID
        OP_CostHead["AccountID"] = this.dao.add_regCost_GetAccountID();
        //填充OP_CostDetail
        var detaildata = this.add_regCost_GetCostDetail(feeHeadIDs) || [];
        detaildata.forEach(function (item) {
            var CostDetail = JSON.parse(JSON.stringify(OP_CostDetail)); //拷贝费用明细对象
            CostDetail['StatID'] = item["StatID"];
            CostDetail['FeeItemHeadID'] = item["FeeItemHeadID"];
            CostDetail['TotalFee'] = item["TotalFee"];
            CostDetail['PresEmpID'] = item['PresEmpID'];
            CostDetail['PresDeptID'] = item['PresDeptID'];
            CostDetail['ExecDeptID'] = item['ExecDeptID'];
            List_CostDetail.push(CostDetail);
        });
        //填充OP_CostPayMentInfo对象数据
        if (parseInt(PayMode) == 1) {
            OP_CostPayMentInfo["PayMentCode"] = "11";
        }
        else if (parseInt(PayMode) == 2) {
            OP_CostPayMentInfo["PayMentCode"] = "12";
        }
        else {
            OP_CostPayMentInfo["PayMentCode"] = "01"; //现金
        }
        OP_CostPayMentInfo["PayMentMoney"] = TotalFee; //支付金额
        //获取PaymentID、PayName
        var paymentdata = this.add_regCost_GetPayMentID(OP_CostPayMentInfo["PayMentCode"]);
        OP_CostPayMentInfo["PayMentID"] = paymentdata['PaymentID'];
        OP_CostPayMentInfo["PayMentName"] = paymentdata['PayName'];
        OP_CostPayMentInfo["TradeOrderNo"] = TranSerNo;
        OP_CostPayMentInfo["PatType"] = OP_CostHead["PatTypeID"];
        OP_CostPayMentInfo["PatListID"] = OP_CostHead["PatListID"];
        OP_CostPayMentInfo["PatName"] = OP_CostHead["PatName"];
        OP_CostPayMentInfo["AccountID"] = OP_CostHead["AccountID"];
        List_CostPayMentInfo.push(OP_CostPayMentInfo);
        //结算数据保存
        this.add_regCost_SaveData(OP_CostHead, List_CostDetail, List_CostPayMentInfo, feeHeadIDs);
        return OP_CostHead;
    };
    //获取OPCostHead
    tshisinterfaceService.prototype.add_regCost_GetOPCostHead = function (Regno, OP_CostHead) {
        var strsql = "SELECT MemberID,MemberAccountID,CardNO,PatListID,PatName,PatTypeID,RegCategory,OperatorID,dbo.fnGetEmpName(OperatorID) OperatorName \
        FROM OP_PatList WHERE PatListID=@Regno";
        var dbparams = {
            Regno: Regno
        };
        var result = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        if (result) {
            OP_CostHead['MemberID'] = result["MemberID"];
            OP_CostHead['MemberAccountID'] = result["MemberAccountID"];
            OP_CostHead['CardNO'] = result["CardNO"];
            OP_CostHead['PatListID'] = result["PatListID"];
            OP_CostHead['PatName'] = result["PatName"];
            OP_CostHead['PatTypeID'] = result["PatTypeID"];
            OP_CostHead['ChargeEmpID'] = result["OperatorID"];
            OP_CostHead['ChargeEmpName'] = result["OperatorName"];
            OP_CostHead['RegCategory'] = result["RegCategory"];
        }
    };
    //获取发票号
    tshisinterfaceService.prototype.add_regCost_GetInvoice = function () {
        var currno;
        var endno;
        var invoiceID;
        var retdata;
        var strsql = "SELECT TOP 1 ID,CurrentNO,PerfChar,EndNO FROM Basic_Invoice where EmpID=@ChargeEmpID and Status in (0,2) and WorkID=@WorkId ORDER BY Status ";
        var dbparams = {
            ChargeEmpID: this.data.ChargeEmpID,
            WorkId: this.data.WorkId
        };
        var result = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        if (result) {
            retdata = { ID: result["ID"], CurrentNO: result["CurrentNO"], PerfChar: result["PerfChar"] };
            invoiceID = result["ID"];
            currno = result["CurrentNO"];
            endno = result["EndNO"];
        }
        var strsql2;
        //发票号+1
        if (currno == endno) {
            strsql2 = "UPDATE Basic_Invoice set Status=1 WHERE ID=@invoiceID";
        }
        else {
            strsql2 = "UPDATE Basic_Invoice set CurrentNO=CurrentNO+1 WHERE ID=@invoiceID";
        }
        var dbparams2 = {
            invoiceID: invoiceID
        };
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql2, dbparams2);
        return retdata;
    };
    //获取总金额
    tshisinterfaceService.prototype.add_regCost_TotalFee = function (feeHeadIDs) {
        var strsql = "SELECT SUM(TotalFee) TotalFee FROM OP_FeeItemHead where FeeItemHeadID in (select id from dbo.fn_SplitStrToTable(@feeHeadIDs))";
        var dbparams = {
            feeHeadIDs: feeHeadIDs.join(',')
        };
        var result = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, dbparams);
        return result;
    };
    //获取结算明细
    tshisinterfaceService.prototype.add_regCost_GetCostDetail = function (feeHeadIDs) {
        var strsql = "SELECT a.StatID,a.FeeItemHeadID,SUM(a.TotalFee) TotalFee,b.PresEmpID,b.PresDeptID,b.ExecDeptID FROM OP_FeeItemDetail a \
                        LEFT JOIN OP_FeeItemHead b on a.FeeItemHeadID=b.FeeItemHeadID \
                        WHERE a.FeeItemHeadID in (select id from   dbo.fn_SplitStrToTable(@feeHeadIDs)) \
                        GROUP BY a.StatID,a.FeeItemHeadID,b.PresEmpID,b.PresDeptID,b.ExecDeptID";
        var dbparams = {
            feeHeadIDs: feeHeadIDs.join(',')
        };
        var result = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams);
        return result;
    };
    //获取PayMentID、PayMentName
    tshisinterfaceService.prototype.add_regCost_GetPayMentID = function (PayCode) {
        var strsql = "SELECT PaymentID,PayCode,PayName FROM Basic_Payment where WorkID=@WorkId and PayCode=@PayCode";
        var dbparams = {
            WorkId: this.data.WorkId,
            PayCode: PayCode
        };
        var result = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        return result;
    };
    //结算数据保存
    tshisinterfaceService.prototype.add_regCost_SaveData = function (OP_CostHead, List_CostDetail, List_CostPayMentInfo, feeHeadIDs) {
        var _this = this;
        var CurrDate = TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss"); //当前日期
        //插入OP_CostHead---------------------------------
        //-------------------------------------------------------------------------------------
        OP_CostHead['CostDate'] = CurrDate;
        var strsql = "INSERT INTO [OP_CostHead]( [MemberID], [MemberAccountID], [CardNO], [PatListID], [PatName], [PatTypeID], [BeInvoiceNO] \
        , [EndInvoiceNO], [ChargeEmpID], [ChargeEmpName], [TotalFee], [CashFee], [PosFee], [PromFee], [RecipeFlag], [CostDate] \
        , [CostStatus], [OldID], [AccountID], [RegFlag], [RegCategory], [RoundingFee], [InvoiceID], [WorkID]) \
        VALUES ( @MemberID, @MemberAccountID, @CardNO, @PatListID, @PatName, @PatTypeID,@BeInvoiceNO, \
        @EndInvoiceNO, @ChargeEmpID, @ChargeEmpName, @TotalFee, @CashFee, @PosFee, @PromFee,@RecipeFlag, @CostDate, \
        @CostStatus,@OldID, @AccountID,@RegFlag, @RegCategory,@RoundingFee, @InvoiceID, @WorkID); \
        SELECT @@IDENTITY;";
        OP_CostHead['CostHeadID'] = parseInt(TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, OP_CostHead));
        //循环插入OP_CostDetail-----------------------------------------
        for (var i = 0; i < List_CostDetail.length; i++) {
            strsql = "INSERT INTO [OP_CostDetail]([CostHeadID], [StatID], [TotalFee], [FeeItemHeadID], [PresEmpID], [PresDeptID], [ExeDeptID], [WorkID]) \
            VALUES (@CostHeadID, @StatID, @TotalFee, @FeeItemHeadID, @PresEmpID, @PresDeptID, @ExeDeptID, @WorkID);";
            List_CostDetail[i]['CostHeadID'] = OP_CostHead['CostHeadID'];
            TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, List_CostDetail[i]);
        }
        //循环插入OP_CostPayMentInfo------------------------------------------
        for (var k = 0; k < List_CostPayMentInfo.length; k++) {
            strsql = "INSERT INTO [OP_CostPayMentInfo]([CostHeadID], [PayMentID], [PayMentName], [PayMentMoney], [AccountID], [PatType] \
                , [PatListID], [PatName], [WorkID], [PayMentCode], [TradeOrderNo], [qlcOrderId]) \
                VALUES (@CostHeadID, @PayMentID, @PayMentName, @PayMentMoney, @AccountID, @PatType, \
                @PatListID, @PatName, @WorkID, @PayMentCode, @TradeOrderNo, @qlcOrderId);";
            List_CostPayMentInfo[k]['CostHeadID'] = OP_CostHead['CostHeadID'];
            TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, List_CostPayMentInfo[k]);
        }
        //回写费用OP_FeeItemHead-------------------------
        var OP_FeeItemHead = {};
        OP_FeeItemHead['CostHeadID'] = OP_CostHead['CostHeadID'];
        OP_FeeItemHead['ChargeEmpID'] = OP_CostHead['ChargeEmpID'];
        OP_FeeItemHead['InvoiceNO'] = OP_CostHead['EndInvoiceNO']; //小票，一次结算多张发票
        OP_FeeItemHead['ReciptNO'] = OP_CostHead['EndInvoiceNO']; //正式发票号，一次结算多张发票
        OP_FeeItemHead['ChargeStatus'] = 0;
        OP_FeeItemHead['ChargeFlag'] = 1;
        OP_FeeItemHead['RoungingFee'] = 0; //舍入金额,分摊到每张发票
        OP_FeeItemHead['DiscountFee'] = 0; //折扣金额
        OP_FeeItemHead['ChargeDate'] = OP_CostHead['CostDate'];
        for (var m = 0; m < feeHeadIDs.length; m++) {
            strsql = "UPDATE [OP_FeeItemHead] SET  \
            [CostHeadID] = @CostHeadID \
            , [ChargeEmpID] = @ChargeEmpID \
            , [InvoiceNO] = @InvoiceNO \
            , [ReciptNO] = @ReciptNO \
            , [ChargeStatus] = @ChargeStatus \
            , [ChargeFlag] = @ChargeFlag \
            , [RoungingFee] = @RoungingFee \
            , [DiscountFee] = @DiscountFee \
            , [ChargeDate] = @ChargeDate \
            WHERE [FeeItemHeadID] = @FeeItemHeadID";
            OP_FeeItemHead['FeeItemHeadID'] = feeHeadIDs[m];
            TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, OP_FeeItemHead);
        }
        //挂号结算需要回写OP_PatList的CostHeadID
        if (parseInt(OP_CostHead['RegFlag']) == 1) {
            strsql = "UPDATE OP_PatList set CostHeadID=@CostHeadID where PatListID=@PatListID";
            var dbparams = {
                CostHeadID: OP_CostHead['CostHeadID'],
                PatListID: OP_CostHead['PatListID']
            };
            TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, dbparams);
        }
        //如果是处方还要回写处方收费状态和申请单表收费状态
        //配发药窗口
        //扣减虚拟库存
        //生成执行单
        if (parseInt(OP_CostHead['RegFlag']) == 0) {
            var dbparams = {
                feeHeadIDs: feeHeadIDs.join(",")
            };
            strsql = "UPDATE OPD_PresDetail set IsCharged=1 WHERE PresDetailID in ( \
                    SELECT b.DocPresDetailID FROM OP_FeeItemHead a LEFT JOIN OP_FeeItemDetail b on a.FeeItemHeadID=b.FeeItemHeadID \
                    where a.FeeItemHeadID in (select id from dbo.fn_SplitStrToTable(@feeHeadIDs)) \
                    );";
            TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, dbparams);
            strsql = "update EXA_MedicalApplyDetail set ApplyStatus=1 WHERE PresDetailID in( \
                    SELECT b.DocPresDetailID FROM OP_FeeItemHead a LEFT JOIN OP_FeeItemDetail b on a.FeeItemHeadID=b.FeeItemHeadID \
                    where a.FeeItemHeadID in (select id from dbo.fn_SplitStrToTable(@feeHeadIDs)) \
                    )";
            TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, dbparams);
            //ItemType=1表示药品需要扣减虚拟库存
            //Amount数量,判断库存是否足够，然后减虚拟库存
            strsql = "SELECT b.ItemID,b.Amount,a.ExecDeptID FROM OP_FeeItemHead a LEFT JOIN OP_FeeItemDetail b on a.FeeItemHeadID=b.FeeItemHeadID \
                    where b.ItemType=1 AND a.FeeItemHeadID in (select id from dbo.fn_SplitStrToTable(@feeHeadIDs))";
            var result = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams) || [];
            result.forEach(function (item) {
                var drugID = item["ItemID"];
                var DeptID = item["ExecDeptID"];
                var Amount = item["Amount"];
                var ValidAmount = _this.add_regCost_GetStorage(drugID, DeptID);
                if (parseFloat(Amount) > parseFloat(ValidAmount)) {
                    throw "Error:虚拟库存不足DrugID=" + drugID;
                }
                _this.add_regCost_UpdateValidStorage(drugID, DeptID, Amount);
            });
        }
    };
    //查询药品的虚拟库存
    tshisinterfaceService.prototype.add_regCost_GetStorage = function (drugID, deptID) {
        var strsql = "SELECT ValidAmount FROM  DS_ValidStorage WHERE DrugID=@drugID and DeptID=@deptID";
        var dbparams = {
            drugID: drugID,
            deptID: deptID
        };
        var result = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, dbparams);
        return result;
    };
    //更新有效库存
    tshisinterfaceService.prototype.add_regCost_UpdateValidStorage = function (drugID, deptID, amount) {
        var strsql = "update DS_ValidStorage set ValidAmount=ValidAmount-@amount  WHERE DrugID=@drugID and DeptID=@deptID";
        var dbparams = {
            drugID: drugID,
            deptID: deptID,
            amount: amount
        };
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, dbparams);
    };
    /**
       新增门诊交款
     
       @param {String} OP_CostHead - OP_CostHead description
       @return {String} return description
    */
    tshisinterfaceService.prototype.add_Account = function (OP_CostHead) {
        var strsql = "UPDATE [OP_Account] SET  \
        [TotalFee] =[TotalFee]+ @TotalFee \
        , [CashFee] =[CashFee]+ @CashFee \
        , [PromFee] =[PromFee]+ @PromFee \
        , [PosFee] =[PosFee]+ @PosFee \
        , [InvoiceCount] =[InvoiceCount]+ 1 \
        , [RefundInvoiceCount] =[RefundInvoiceCount]+ 0 \
        , [RoundingFee] =[RoundingFee]+ @RoundingFee \
        WHERE [AccountID] = @AccountID";
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, OP_CostHead);
    };
    return tshisinterfaceService;
}());
//挂号
var registerService = /** @class */ (function () {
    function registerService(workid, dbconn, patTypeID, chargeEmpID, chargeEmpName) {
        this.data = {
            dbConn: null,
            WorkId: 1,
            PatTypeID: 100,
            ChargeEmpID: 1322,
            ChargeEmpName: '机器人'
        };
        this.data.WorkId = workid;
        this.data.dbConn = dbconn;
        this.data.PatTypeID = patTypeID;
        this.data.ChargeEmpID = chargeEmpID;
        this.data.ChargeEmpName = chargeEmpName;
        this.service = new tshisinterfaceService(workid, dbconn, patTypeID, chargeEmpID, chargeEmpName);
        this.dao = new tshisinterfaceDao(workid, dbconn, chargeEmpID, chargeEmpName);
    }
    //挂号
    registerService.prototype.hisregister = function (DeptId, DoctId, Regid, RegDate, Timeid, CardType, CardNum, TranSerNo, PayTime, PayMode, TotalRegFee) {
        var DeptAddress = this.service.getDeptAddress(DeptId) || ""; //返回挂号科室地址
        //今日挂号
        //0.判断是否已过时间段，比如：当前时间不能挂上午的号
        //1.排班数据是否有效，根据regid判断
        //2.判断号源是否满
        //3.判断病人卡号是否有效
        //5.执行挂号（OP_PatList、OP_FeeItemHead、OP_FeeItemDetail、OP_CostHead、OP_CostDetail、OP_CostPayMentInfo、OP_Account）
        //6.插入候诊队列
        //预约挂号
        //1.排班数据是否有效，根据regid判断
        //2.判断号源是否满
        //3.判断病人卡号是否有效
        //4.执行挂号（OP_PatList、OP_FeeItemHead、OP_FeeItemDetail、OP_CostHead、OP_CostDetail、OP_CostPayMentInfo、OP_Account）
        try {
            var today = TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss").substring(0, 10);
            var curDate = new Date(today);
            var regDate = new Date(RegDate);
            var result;
            this.data.dbConn.BeginTransaction();
            if (regDate.getTime() == curDate.getTime()) { //今日挂号
                // if (!this.service.CheckTodayTime(Timeid)) {
                //     throw "当前时间不能挂此时段的号";
                // }
                // if (!this.service.CheckSchedule(Regid, DeptId, DoctId)) {
                //     throw "当前医生排班已经停用";
                // }
                // if (!this.service.CheckFull(Regid)) {
                //     throw "当前医生时段号源已满";
                // }
                if (!this.service.CheckPatient(CardType, CardNum)) {
                    throw "当前就诊病人无效";
                }
                var resultaddreg = this.add_regRecord(CardType, CardNum, DeptId, DoctId, Regid, Timeid, RegDate); //增加挂号记录
                var resultaddfee = this.service.add_regFee(0, resultaddreg.Regno, ''); //增加门诊费用
                var feeHeadIDs = [];
                feeHeadIDs.push(resultaddfee.FeeItemHeadID);
                var resultaddcost = this.service.add_regCost(0, resultaddreg.Regno, feeHeadIDs, TranSerNo, PayTime, PayMode, TotalRegFee); //增加门诊结算
                this.service.add_Account(resultaddcost); //增加门诊交款
                //this.enter_waitingroom(resultaddreg.Regno);//进入候诊队列 
                result = { result: true, Regno: resultaddreg.Regno, Location: DeptAddress };
            }
            else if (regDate.getTime() > curDate.getTime()) { //预约挂号
                // if (!this.service.CheckSchedule(Regid, DeptId, DoctId)) {
                //     throw "当前医生排班已经停用";
                // }
                // if (!this.service.CheckFull(Regid)) {
                //     throw "当前医生时段号源已满";
                // }
                if (!this.service.CheckPatient(CardType, CardNum)) {
                    throw "当前就诊病人无效";
                }
                var resultaddreg = this.add_regRecord(CardType, CardNum, DeptId, DoctId, Regid, Timeid, RegDate); //增加挂号记录
                var resultaddfee = this.service.add_regFee(0, resultaddreg.Regno, ''); //增加门诊费用
                var feeHeadIDs = [];
                feeHeadIDs.push(resultaddfee.FeeItemHeadID);
                var resultaddcost = this.service.add_regCost(0, resultaddreg.Regno, feeHeadIDs, TranSerNo, PayTime, PayMode, TotalRegFee); //增加门诊结算
                this.service.add_Account(resultaddcost); //增加门诊交款
                result = { result: true, Regno: resultaddreg.Regno, Location: DeptAddress };
            }
            else { //无效预约日期
                throw 'Error: regdate is Invalid';
            }
            this.data.dbConn.CommitTransaction();
            return result;
        }
        catch (e) {
            this.data.dbConn.RollbackTransaction();
            throw e;
        }
    };
    /**
         新增挂号记录
        //1.先根据卡号查询病人信息、门诊号
        //2.插入OP_PatList
        //3.获取Regno
        @param {String} CardType, CardNum, DeptId, DoctId, Regid, Timeid, regDate,PatTypeID,ChargeEmpID
        @return {String} return 返回挂号对象
        */
    registerService.prototype.add_regRecord = function (CardType, CardNum, DeptId, DoctId, Regid, Timeid, regDate) {
        //病人信息------------------------------------------
        var strsql = "SELECT TOP 1 a.MemberID,b.AccountID MemberAccountID,b.CardNO,a.Name PatName, dbo.fnGetSex(a.Sex) PatSex,a.Birthday,'Y'+ CONVERT(char(10),dbo.fnGetAgeNumber(Birthday)) Age,a.IDNumber,a.WorkUnit,a.Allergies FROM \
                        ME_MemberInfo a INNER JOIN ME_MemberAccount b on a.MemberID=b.MemberID \
                        where b.CardTypeID=@CardType and b.CardNO=@CardNum and b.WorkID=@WorkId";
        var dbparams = {
            CardType: CardType,
            CardNum: CardNum,
            WorkId: this.data.WorkId
        };
        var OP_PatList = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        //获取RegTypeID-----------------------------------------
        var regtypedata = this.add_regRecord_RegTypeID(Regid);
        OP_PatList["RegTypeID"] = regtypedata.RegTypeID;
        OP_PatList["RegTypeName"] = regtypedata.RegTypeName;
        //获取门诊号--------------------------------------------
        OP_PatList["VisitNO"] = this.add_regRecord_VisitNO();
        OP_PatList["RegDeptID"] = DeptId;
        OP_PatList["RegEmpID"] = DoctId;
        OP_PatList["CureDeptID"] = DeptId;
        OP_PatList["CureEmpID"] = DoctId;
        OP_PatList["RegDocName"] = this.service.getEmpName(DoctId);
        OP_PatList["RegDeptName"] = this.service.getDeptName(DeptId);
        OP_PatList["RegDate"] = regDate;
        OP_PatList["RegTimeRange"] = Timeid;
        OP_PatList["PatTypeID"] = this.data.PatTypeID;
        OP_PatList["OperatorID"] = this.data.ChargeEmpID; //操作人
        OP_PatList["WorkID"] = this.data.WorkId;
        //TrackLog("add_regRecord", 136, JSON.stringify(OP_PatList));
        this.add_regRecord_savedata(OP_PatList); //保存挂号数据
        //返回挂号对象
        return OP_PatList;
    };
    //获取RegTypeID
    registerService.prototype.add_regRecord_RegTypeID = function (Regid) {
        var strsql = "SELECT TOP 1 a.RegTypeID,a.RegTypeName,a.DoctorPostCode,SUM(c.Price) Amount FROM OP_RegType a \
                            LEFT JOIN OP_RegItemFee b on a.RegTypeID=b.RegTypeID and a.WorkID=b.WorkID \
                            LEFT JOIN Basic_HospFeeItem c on b.ItemID=c.ItemID and b.WorkID=c.WorkID \
                            WHERE b.Flag=0 AND a.WorkID=1 and a.DoctorPostCode=(SELECT TOP 1 DocProfessionCode FROM OP_DocSchedual where schedualID=@Regid) \
                            GROUP BY a.RegTypeID,a.RegTypeName,a.DoctorPostCode";
        var dbparams = {
            Regid: Regid
        };
        var result = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        return result;
    };
    //获取门诊号
    registerService.prototype.add_regRecord_VisitNO = function () {
        var nowdate = TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss").substring(0, 10);
        //如果Basic_SerialNumberSource表里没有门诊流水号就插入一条门诊流水号数据
        //如果今天日期就获取当前序号+1
        //如果不是今天就改为今天日期并序号改为1
        var VisitNO = ""; //20201227001
        var strsql = "SELECT CONVERT(varchar(10), CurrDate, 120) CurrDate,RIGHT('000'+CONVERT(VARCHAR(50),CurrSequence+1),3) CurrSequence \
            FROM Basic_SerialNumberSource WHERE SNType=1 AND WorkID=@WorkId AND DeptId=0";
        var params = {
            WorkId: this.data.WorkId
        };
        var result = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, params);
        if (result) {
            var currdate = result.CurrDate;
            var currseq = result.CurrSequence;
            if (currdate == nowdate) {
                strsql = "UPDATE Basic_SerialNumberSource set CurrSequence=CurrSequence+1 WHERE SNType=1 AND WorkID=@WorkId AND DeptId=0";
                TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, params);
                VisitNO = nowdate.replace(/-/g, "") + currseq;
            }
            else {
                strsql = "UPDATE Basic_SerialNumberSource set CurrDate=GETDATE(),CurrSequence=1 WHERE SNType=1 AND WorkID=@WorkId AND DeptId=0";
                TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, params);
                VisitNO = nowdate.replace(/-/g, "") + "001";
            }
        }
        else {
            strsql = "INSERT INTO Basic_SerialNumberSource([SNType], [BusinessType], [DeptId], [CurrDate], [CurrSequence], [WorkID]) \
                            VALUES (1, '', 0, GETDATE(), 1, @WorkId);";
            TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, params);
            VisitNO = nowdate.replace(/-/g, "") + "001";
        }
        return VisitNO;
    };
    //保存挂号数据
    registerService.prototype.add_regRecord_savedata = function (OP_PatList) {
        //插入OP_PatList---------------------------------------
        var strsql = "INSERT INTO [OP_PatList]( [MemberID], [MemberAccountID], [CardNO], [PatTypeID], [VisitNO], [RegTypeID], [RegTypeName] \
        , [RegDeptID], [RegEmpID], [RegDocName], [RegDeptName], [PatName], [PatSex], [Birthday], [Age], [PyCode], [WbCode] \
        , [CureDeptID], [CureEmpID], [DiseaseCode], [DiseaseName], [RegDate], [VisitStatus], [CostHeadID], [ChargeFlag], [RegCategory], [RegStatus], [RegTimeRange], [RegSourceNo] \
        , [WorkID], [IDNumber], [WorkUnit], [Allergies], [PatTypeName], [OperatorID], [MedicareCard], [IsNew], [IsEmergency], [IsOffer], [IsYy], [YyDate], [ReceiptNo]) \
                VALUES (@MemberID, @MemberAccountID, @CardNO, @PatTypeID, @VisitNO, @RegTypeID, @RegTypeName \
                    , @RegDeptID, @RegEmpID, @RegDocName, @RegDeptName, @PatName, @PatSex, @Birthday, @Age, '', '' \
                    , @CureDeptID, @CureEmpID, '', '', @RegDate, 0, 0, 0, 0, 0, @RegTimeRange, 0, \
                    @WorkID, @IDNumber, @WorkUnit, @Allergies, '全自费', @OperatorID, '', 1, 0, 0, 1, NULL, ''); \
                SELECT @@IDENTITY;";
        var result = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, OP_PatList);
        OP_PatList["PatListID"] = parseInt(result);
        OP_PatList["Regno"] = parseInt(result);
    };
    return registerService;
}());
//取消挂号
var cancelregisterService = /** @class */ (function () {
    function cancelregisterService(workid, dbconn, patTypeID, chargeEmpID, chargeEmpName) {
        this.data = {
            dbConn: null,
            WorkId: 1,
            PatTypeID: 100,
            ChargeEmpID: 1322,
            ChargeEmpName: '机器人'
        };
        this.data.WorkId = workid;
        this.data.dbConn = dbconn;
        this.data.ChargeEmpID = chargeEmpID;
        this.data.ChargeEmpName = chargeEmpName;
        this.service = new tshisinterfaceService(workid, dbconn, patTypeID, chargeEmpID, chargeEmpName);
        this.dao = new tshisinterfaceDao(workid, dbconn, chargeEmpID, chargeEmpName);
    }
    //取消挂号
    cancelregisterService.prototype.cancelregister = function (Regno, RegDate) {
        //1获取票据号
        var cost = this.getCost(Regno);
        if (!cost && !cost.EndInvoiceNO) {
            throw "找不到该票据号信息!";
        }
        if (cost.CostStatus != 0) {
            throw "该票据号已经退费，不能再退";
        }
        var oldCostHeadID = cost.CostHeadID;
        //2 就诊记录
        var pat = this.getpatlist(Regno);
        var curDate = new Date(TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss").substring(0, 10));
        var regDate = new Date(RegDate);
        if (pat.VisitStatus != 0) {
            throw "您已就诊，不能再退号";
        }
        else if (regDate.getTime() < curDate.getTime()) {
            throw "已超过退号日期，不能再退";
        }
        //OP_PatList
        this.savePatList(Regno);
        var accountId = this.dao.add_regCost_GetAccountID();
        //取消操作
        var newcostHead = this.cancelCost(cost, accountId);
        //LobsterAPI.TrackLog("cancelregister", "333333");
        this.cancelFee(oldCostHeadID, newcostHead.CostHeadID);
        // LobsterAPI.TrackLog("cancelregister", "44444");
        return { result: true };
    };
    //查询票据号
    cancelregisterService.prototype.getCost = function (Regno) {
        var strsql = "SELECT\
        CostHeadID CostHeadID,  MemberID MemberID,   MemberAccountID MemberAccountID, CardNO CardNO,\
        PatListID PatListID, PatName PatName, PatTypeID PatTypeID, BeInvoiceNO BeInvoiceNO, EndInvoiceNO EndInvoiceNO,\
        ChargeEmpID ChargeEmpID, ChargeEmpName ChargeEmpName, TotalFee TotalFee, CashFee CashFee, PosFee PosFee,\
        PromFee PromFee, RecipeFlag RecipeFlag, CostDate CostDate, CostStatus CostStatus, OldID OldID, AccountID AccountID,\
        RegFlag RegFlag, RegCategory RegCategory, RoundingFee RoundingFee, InvoiceID InvoiceID, WorkID WorkID\
        FROM dbo.OP_CostHead where PatListId=@Regno and regFlag=1";
        var dbparams = {
            Regno: Regno
        };
        var result = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        return result;
    };
    //获取就诊记录
    cancelregisterService.prototype.getpatlist = function (patlistId) {
        var strsql = "  SELECT PatListID ,MemberID ,MemberAccountID ,CardNO ,PatTypeID ,VisitNO\
          ,RegTypeID ,RegTypeName ,RegDeptID ,RegEmpID ,RegDocName ,RegDeptName\
          ,PatName ,PatSex ,Birthday ,Age ,PyCode  ,WbCode ,CureDeptID ,CureEmpID\
          ,DiseaseCode ,DiseaseName ,RegDate ,VisitStatus ,CostHeadID ,ChargeFlag\
          ,RegCategory ,RegStatus ,RegTimeRange ,RegSourceNo ,WorkID ,IDNumber ,WorkUnit\
          ,Allergies ,PatTypeName ,OperatorID ,MedicareCard ,IsNew ,IsEmergency ,IsOffer\
          ,IsYy ,YyDate ,ReceiptNo FROM  OP_PatList\
          where PatListID=@patlistId ";
        var Info = {};
        var dbparams = {
            patlistId: patlistId
        };
        var result = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        return result;
    };
    //原挂号记录状态置1
    cancelregisterService.prototype.savePatList = function (patlistId) {
        var strsql = "UPDATE OP_PatList set RegStatus=1 where PatListID=@patlistId";
        var dbparams = {
            patlistId: patlistId
        };
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, dbparams);
    };
    /**
    门诊取消结算
        
    @param {String} arg1 - arg1 description
    @return {String} return description
    */
    cancelregisterService.prototype.cancelCost = function (cost, accountId) {
        var oldCostId = cost.CostHeadID;
        //修改老的costhead表
        this.updateOldCost(cost.CostHeadID);
        //查找原来的支付方式信息 
        var oldCostPayList = this.getcostpay(cost.CostHeadID);
        var listPatTypePayMent = this.getPatTypePayment(cost.PatTypeID);
        var newcost = this.initNewCostInfo(cost, oldCostPayList, listPatTypePayMent);
        var curDate = new Date(TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss").substring(0, 10));
        //插入新的costhead表
        newcost.CostHeadID = 0;
        newcost.CostStatus = 2;
        newcost.OldID = oldCostId;
        newcost.CostDate = curDate.getTime();
        newcost.ChargeEmpID = this.data.ChargeEmpID;
        newcost.AccountID = accountId;
        var newCostHeadId = this.addCost(newcost);
        newcost.CostHeadID = newCostHeadId;
        this.dealPayMent(listPatTypePayMent, oldCostPayList, newcost, accountId);
        return newcost;
    };
    /**
        门诊退费
         
        @param {String} arg1 - arg1 description
        @return {String} return description
        */
    cancelregisterService.prototype.cancelFee = function (costHeadID, newCostHeadID) {
        //旧的feeitemhead
        var listFeeitemHead = this.getOldOpFeeHead(costHeadID);
        //旧的
        var listFeeItemDetail;
        if (listFeeitemHead.length > 0) {
            listFeeItemDetail = this.getOldOpFeeHeadDetail(listFeeitemHead[0]["FeeItemHeadID"]);
        }
        //修改旧的FeeItemhead 
        this.modifyFeeItemHead(costHeadID);
        //add new FeeItemHead
        var feeItemId = this.newFeeItemHead(listFeeitemHead[0], newCostHeadID, listFeeitemHead[0].TotalFee);
        //新增新的OP_FeeItemDetail 
        this.newFeeItemDetail(listFeeItemDetail, feeItemId);
        //新增CostDetail表数据
        this.newCostDetail(costHeadID, newCostHeadID, feeItemId);
    };
    //原op_costHead变化
    cancelregisterService.prototype.updateOldCost = function (costheadId) {
        var strsql = "UPDATE OP_CostHead set CostStatus=1 where CostHeadID=@costheadId";
        var dbparams = {
            costheadId: costheadId
        };
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, dbparams);
    };
    //查找原来的支付方式
    cancelregisterService.prototype.getcostpay = function (costHeadId) {
        var strsql = "SELECT ID ,CostHeadID ,PayMentID ,PayMentName ,PayMentMoney ,AccountID ,PatType  ,PatListID\
                        ,PatName ,WorkID ,PayMentCode ,TradeOrderNo ,qlcOrderId FROM dbo.OP_CostPayMentInfo where CostHeadID=@costHeadId";
        var dbparams = {
            costHeadId: costHeadId
        };
        var result = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams);
        return result;
    };
    //获取支付方式退费时返回的方式
    cancelregisterService.prototype.getPatTypePayment = function (pattypeId) {
        var strsql = "SELECT ID ,PatTypeID  ,PaymentID  ,UseType ,PayOrder ,WorkID ,RefundmentID FROM dbo.Basic_PatTypePayment\
                            where WorkID=1 AND PatTypeID=@pattypeId AND PaymentID<>1002 AND UseType=0";
        var dbparams = {
            pattypeId: pattypeId
        };
        var result = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams);
        return result;
    };
    //设置newcost的金额部分
    cancelregisterService.prototype.initNewCostInfo = function (oldCostHead, oldCostPayList, listPatTypePayMent) {
        var newCostHead = oldCostHead;
        newCostHead.CostHeadID = 0;
        //查找结算方式是否配置了pos支付方式，如果没有配置，则直接按现金退回，如果配置了，则按设置方式退回
        var posPayMent = TSLobsterAPI.Utils.findlist(listPatTypePayMent, 'PaymentID', '1004');
        if (posPayMent.length > 0) {
            var refundCashNum_1 = 0;
            var isPosFee = false;
            listPatTypePayMent.forEach(function (item) {
                //退费按现金退
                if (item.RefundmentID == 2) {
                    //如果退现金方式存在pos付款的,则退款结算记录中的pos费用为0，否则为pos支付的费用
                    if (item.PaymentID == 1004) {
                        isPosFee = true;
                    }
                    var newlistPayInfo = TSLobsterAPI.Utils.findlist(oldCostPayList, 'PaymentID', item.PaymentID);
                    //一次结算中一种支付方式只会存在一条结算明细记录
                    newlistPayInfo.forEach(function (data) {
                        refundCashNum_1 = refundCashNum_1 + parseFloat(data['PayMentMoney']);
                    });
                }
            });
            if (isPosFee) {
                newCostHead.PosFee = 0;
            }
            else {
                newCostHead.PosFee = parseFloat(oldCostHead.PosFee) * -1;
            }
            refundCashNum_1 = refundCashNum_1 + parseFloat(oldCostHead.CashFee);
            newCostHead.CashFee = refundCashNum_1 * -1;
        }
        else {
            newCostHead.CashFee = (parseFloat(oldCostHead.CashFee) + parseFloat(oldCostHead.PosFee)) * (-1); //退金额，POS金额退现金
            newCostHead.PosFee = 0;
        }
        newCostHead.PromFee = parseFloat(oldCostHead.PromFee) * (-1);
        newCostHead.OldID = oldCostHead.CostHeadID; //写入原退记录ID
        newCostHead.TotalFee = parseFloat(newCostHead.TotalFee) * (-1);
        return newCostHead;
    };
    //插入costHead
    cancelregisterService.prototype.addCost = function (OP_CostHead) {
        //插入OP_CostHead---------------------------------
        OP_CostHead['CostDate'] = TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss"); //当前日期
        var strsql = "INSERT INTO [OP_CostHead]( [MemberID], [MemberAccountID], [CardNO], [PatListID], [PatName], [PatTypeID], [BeInvoiceNO] \
        , [EndInvoiceNO], [ChargeEmpID], [ChargeEmpName], [TotalFee], [CashFee], [PosFee], [PromFee], [RecipeFlag], [CostDate] \
        , [CostStatus], [OldID], [AccountID], [RegFlag], [RegCategory], [RoundingFee], [InvoiceID], [WorkID]) \
        VALUES ( @MemberID, @MemberAccountID, @CardNO, @PatListID, @PatName, @PatTypeID, @BeInvoiceNO, \
        @EndInvoiceNO, @ChargeEmpID, @ChargeEmpName, @TotalFee, @CashFee, @PosFee, @PromFee, @RecipeFlag, @CostDate, \
        @CostStatus,@OldID, @AccountID,@RegFlag, @RegCategory,@RoundingFee, @InvoiceID, @WorkID); \
        SELECT @@IDENTITY;";
        OP_CostHead['CostHeadID'] = parseInt(TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, OP_CostHead));
        return OP_CostHead['CostHeadID'];
    };
    //处理支付方式
    cancelregisterService.prototype.dealPayMent = function (listPatTypePayMent, oldCostPayList, newCostHead, curAccountId) {
        var List_CostPayMentInfo = [];
        var posPayMent = TSLobsterAPI.Utils.findlist(listPatTypePayMent, 'PaymentID', '1004');
        ////退款排除现金付款方式，其余按原路径退回的
        if (posPayMent.length > 0) {
            listPatTypePayMent.forEach(function (data) {
                if (data.RefundmentID == 1) {
                    var CostPayList = oldCostPayList.filter(function (info, index, array) {
                        return info.PaymentID == data.PaymentID;
                    });
                    CostPayList.forEach(function (info) {
                        var newCostPay = info;
                        newCostPay.ID = 0;
                        newCostPay.CostHeadID = newCostHead.CostHeadID;
                        newCostPay.AccountID = curAccountId;
                        newCostPay.WorkID = newCostHead.WorkID;
                        //退现金另外处理
                        newCostPay.PayMentMoney = parseFloat(info['PayMentMoney']) * -1;
                        List_CostPayMentInfo.push(newCostPay);
                    });
                }
            });
            if (newCostHead.CashFee != 0) {
                var cashCostPay = {
                    CostHeadID: newCostHead.CostHeadID,
                    AccountID: curAccountId,
                    PatListID: newCostHead.PatListID,
                    PatName: newCostHead.PatName,
                    PatType: newCostHead.PatTypeID,
                    PayMentCode: "01",
                    PayMentID: 1002,
                    PayMentMoney: newCostHead.CashFee,
                    PayMentName: "现金支付",
                    WorkID: newCostHead.WorkID
                };
                List_CostPayMentInfo.push(cashCostPay);
            }
        }
        else {
            var oldposfee_1 = 0;
            oldCostPayList.forEach(function (data) {
                if (data.PayMentCode == "02") {
                    oldposfee_1 = parseFloat(data.PayMentMoney);
                }
            });
            oldCostPayList.forEach(function (data) {
                var newCostPayInfo = data;
                newCostPayInfo.AccountID = curAccountId;
                newCostPayInfo.ID = 0;
                newCostPayInfo.CostHeadID = newCostHead.CostHeadID;
                //POS
                if (data.PayMentCode == "02") {
                    newCostPayInfo.PayMentMoney = 0;
                }
                //现金
                if (data.PayMentCode == "01") {
                    newCostPayInfo.PayMentMoney = (parseFloat(data.PayMentMoney) + oldposfee_1) * (-1);
                }
                else {
                    newCostPayInfo.PayMentMoney = parseFloat(data.PayMentMoney) * (-1);
                }
                newCostPayInfo.WorkID = newCostHead.WorkID;
                List_CostPayMentInfo.push(newCostPayInfo);
            });
        }
        this.savepayment(List_CostPayMentInfo);
        //结账表插入汇总金额
        this.addAccoutFee(newCostHead, curAccountId, 0, 1);
        //队列表中更改为退号状态
        //self.setQueuePatStatus(newCostHead.PatListID);
    };
    //写入支付方式
    cancelregisterService.prototype.savepayment = function (List_CostPayMentInfo) {
        //循环插入OP_CostPayMentInfo------------------------------------------
        for (var k = 0; k < List_CostPayMentInfo.length; k++) {
            var strsql = "INSERT INTO [OP_CostPayMentInfo]([CostHeadID], [PayMentID], [PayMentName], [PayMentMoney], [AccountID], [PatType] \
                , [PatListID], [PatName], [WorkID], [PayMentCode], [TradeOrderNo], [qlcOrderId]) \
                VALUES (@CostHeadID, @PayMentID, @PayMentName, @PayMentMoney, @AccountID, @PatType, \
                @PatListID, @PatName, @WorkID, @PayMentCode, @TradeOrderNo, @qlcOrderId);";
            TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, List_CostPayMentInfo[k]);
        }
    };
    //组装opaccount 数据 并保存
    cancelregisterService.prototype.addAccoutFee = function (costHead, curAccountId, invoiceCount, refundInvoiceCount) {
        var opaccount = this.getOp_account(curAccountId);
        opaccount.TotalFee = parseFloat(opaccount.TotalFee) + parseFloat(costHead.TotalFee);
        opaccount.CashFee = parseFloat(opaccount.CashFee) + parseFloat(costHead.CashFee);
        opaccount.PosFee = parseFloat(opaccount.PosFee) + parseFloat(costHead.PosFee);
        opaccount.PromFee = parseFloat(opaccount.PromFee) + parseFloat(costHead.PromFee);
        opaccount.RoundingFee = parseFloat(opaccount.RoundingFee) + parseFloat(costHead.RoundingFee);
        opaccount.InvoiceCount = parseFloat(opaccount.InvoiceCount) + parseFloat(invoiceCount);
        opaccount.RefundInvoiceCount = parseFloat(opaccount.RefundInvoiceCount) + parseFloat(refundInvoiceCount);
        this.saveOpAccount(opaccount);
    };
    //获取Op_account对象
    cancelregisterService.prototype.getOp_account = function (curAccountId) {
        var strsql = "SELECT AccountID,LastDate,TotalFee,CashFee,PromFee,PosFee,AccountEmpID,AccountDate,AccountFlag\
                    ,ReceivFlag,ReceivEmpID,ReceivDate,ReceivBillNO,WorkID,InvoiceCount,RefundInvoiceCount,RoundingFee,AccountType\
                    ,AccountEmpName FROM dbo.OP_Account where AccountID=@curAccountId";
        var dbparams = {
            curAccountId: curAccountId
        };
        var result = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        return result;
    };
    //保存opaccount数据
    cancelregisterService.prototype.saveOpAccount = function (opaccount) {
        var strsql = "update OP_Account set TotalFee=@TotalFee,CashFee=@CashFee,PosFee=@PosFee,PromFee=@PromFee,RoundingFee=@RoundingFee,InvoiceCount=@InvoiceCount,RefundInvoiceCount=@RefundInvoiceCount \
                    where AccountID=@AccountID";
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, opaccount);
    };
    //查询旧的feehead
    cancelregisterService.prototype.getOldOpFeeHead = function (costheadid) {
        var strsql = "SELECT FeeItemHeadID ,CostHeadID ,MemberID ,PatListID ,PatName ,PresType ,PresEmpID ,PresDeptID\
            ,ExecEmpID ,ExecDeptID ,ChargeEmpID ,PresAmount ,TotalFee ,OldID ,InvoiceNO ,ReciptNO ,ChargeStatus,ChargeFlag\
            ,DistributeFlag ,ExecDate ,PresDate ,RoungingFee ,DocPresHeadID ,DiscountFee ,FeeNo ,ChargeDate ,WorkID ,RegFlag\
            ,DocPresNO ,DosageAllotmentID ,DosageMedicalStatus ,DosageMedicalDate ,DosageMedicalDEmp ,SendAllotmentID\
            FROM dbo.OP_FeeItemHead where CostHeadID=@costheadid";
        var dbparams = {
            costheadid: costheadid
        };
        var result = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams);
        return result;
    };
    //查询旧的OP_FeeItemDetail
    cancelregisterService.prototype.getOldOpFeeHeadDetail = function (opfeeHeadId) {
        var self = this;
        var strsql = "SELECT PresDetailID ,FeeItemHeadID ,MemberID ,PatListID ,ItemID ,ItemType ,StatID ,ItemName ,Spec\
                        ,PackUnit ,UnitNO ,StockPrice ,RetailPrice ,Amount ,PresAmount ,TotalFee ,ExamItemID ,DocPresDetailID\
                        ,WorkID ,MiniUnit ,Memo ,DocPresDate FROM dbo.OP_FeeItemDetail\
                        where FeeItemHeadID=@opfeeHeadId";
        var Items = []; //Packages.java.util.ArrayList()
        var dbparams = {
            opfeeHeadId: opfeeHeadId
        };
        var result = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams);
        return result;
    };
    //修改旧的FeeHead
    cancelregisterService.prototype.modifyFeeItemHead = function (costheadId) {
        var strsql = "UPDATE OP_FeeItemHead set ChargeStatus=1 where CostHeadID=@costheadId";
        var dbparams = {
            costheadId: costheadId
        };
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, dbparams);
    };
    //添加费用头表
    cancelregisterService.prototype.newFeeItemHead = function (feeItemHead, newcostheadId, newFeeItemTotal) {
        var OP_FeeItemHead = {
            "FeeItemHeadID": 0,
            "CostHeadID": 0,
            "MemberID": 0,
            "PatListID": 0,
            "PatName": "",
            "PresType": "0",
            "PresEmpID": 0,
            "PresDeptID": 0,
            "ExecEmpID": 0,
            "ExecDeptID": 0,
            "ChargeEmpID": this.data.ChargeEmpID,
            "PresAmount": 1,
            "TotalFee": 0,
            "OldID": 0,
            "InvoiceNO": "",
            "ReciptNO": "",
            "ChargeStatus": 0,
            "ChargeFlag": 0,
            "DistributeFlag": 0,
            "ExecDate": "",
            "PresDate": "",
            "RoungingFee": 0,
            "DocPresHeadID": 0,
            "DiscountFee": 0,
            "FeeNo": 0,
            "ChargeDate": "",
            "WorkID": this.data.WorkId,
            "RegFlag": 1,
            "DocPresNO": "",
            "DosageAllotmentID": 0,
            "DosageMedicalStatus": 0,
            "DosageMedicalDate": "",
            "DosageMedicalDEmp": 0,
            "SendAllotmentID": 0
        };
        OP_FeeItemHead = feeItemHead;
        OP_FeeItemHead.CostHeadID = newcostheadId;
        OP_FeeItemHead.TotalFee = parseFloat(newFeeItemTotal) * (-1);
        OP_FeeItemHead.OldID = feeItemHead.FeeItemHeadID;
        return this.add_regFee_FeeItemHead(OP_FeeItemHead);
    };
    //插入FeeItemHead
    cancelregisterService.prototype.add_regFee_FeeItemHead = function (OP_FeeItemHead) {
        //插入头表
        var strsql = "INSERT INTO [OP_FeeItemHead]([CostHeadID], [MemberID], [PatListID], [PatName], [PresType], [PresEmpID], [PresDeptID] \
                        , [ExecEmpID], [ExecDeptID], [ChargeEmpID], [PresAmount], [TotalFee], [OldID], [InvoiceNO], [ReciptNO], [ChargeStatus] \
                        , [ChargeFlag], [DistributeFlag], [ExecDate], [PresDate], [RoungingFee], [DocPresHeadID], [DiscountFee], [FeeNo], [ChargeDate] \
                        , [WorkID], [RegFlag], [DocPresNO], [DosageAllotmentID], [DosageMedicalStatus], [DosageMedicalDate], [DosageMedicalDEmp], [SendAllotmentID]) \
                    VALUES (@CostHeadID, @MemberID, @PatListID, @PatName, @PresType, @PresEmpID, @PresDeptID, \
                    @ExecEmpID, @ExecDeptID, @ChargeEmpID, @PresAmount, @TotalFee, @OldID, @InvoiceNO, @ReciptNO, @ChargeStatus, \
                    @ChargeFlag, @DistributeFlag, @ExecDate, @PresDate,@RoungingFee, @DocPresHeadID,@DiscountFee,@FeeNo, @ChargeDate, \
                    @WorkID, @RegFlag, @DocPresNO, @DosageAllotmentID, @DosageMedicalStatus, @DosageMedicalDate, @DosageMedicalDEmp, @SendAllotmentID); \
                    SELECT @@IDENTITY;";
        OP_FeeItemHead['FeeItemHeadID'] = parseInt(TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, OP_FeeItemHead));
        return OP_FeeItemHead['FeeItemHeadID'];
    };
    //添加费用明细表
    cancelregisterService.prototype.newFeeItemDetail = function (List_FeeItemDetail, FeeItemHeadID) {
        var OP_FeeItemDetail = {
            "PresDetailID": 0,
            "FeeItemHeadID": 0,
            "MemberID": 0,
            "PatListID": 0,
            "ItemID": 0,
            "ItemType": 0,
            "StatID": 0,
            "ItemName": "",
            "Spec": "",
            "PackUnit": "",
            "UnitNO": 1,
            "StockPrice": 0,
            "RetailPrice": 0,
            "Amount": 1,
            "PresAmount": 1,
            "TotalFee": 0,
            "ExamItemID": 0,
            "DocPresDetailID": 0,
            "WorkID": this.data.WorkId,
            "MiniUnit": "",
            "Memo": "",
            "DocPresDate": TSLobsterAPI.Utils.getCurrTime('yyyy-MM-dd HH:mm:ss') //当前日期
        }; //费用明细表
        List_FeeItemDetail.forEach(function (data) {
            data = OP_FeeItemDetail;
            data.FeeItemHeadID = FeeItemHeadID;
            data.TotalFee = parseFloat(data.TotalFee) * (-1);
            data.Amount = parseFloat(data.Amount) * (-1);
        });
        this.add_regFee_FeeItemDetail(List_FeeItemDetail);
    };
    //插入FeeItemDetail
    cancelregisterService.prototype.add_regFee_FeeItemDetail = function (List_FeeItemDetail) {
        for (var i = 0; i < List_FeeItemDetail.length; i++) {
            var strsql = "INSERT INTO [OP_FeeItemDetail]([FeeItemHeadID], [MemberID], [PatListID], [ItemID], [ItemType], [StatID] \
                            , [ItemName], [Spec], [PackUnit], [UnitNO], [StockPrice], [RetailPrice], [Amount], [PresAmount], [TotalFee] \
                            , [ExamItemID], [DocPresDetailID], [WorkID], [MiniUnit], [Memo], [DocPresDate]) \
                        VALUES (@FeeItemHeadID, @MemberID, @PatListID, @ItemID, @ItemType, @StatID, \
                        @ItemName, @Spec, @PackUnit, @UnitNO, @StockPrice, @RetailPrice, @Amount, @PresAmount, @TotalFee, \
                        @ExamItemID, @DocPresDetailID, @WorkID, @MiniUnit, @Memo, @DocPresDate);";
            TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, List_FeeItemDetail[i]);
        }
    };
    //添加新的Costdetail
    cancelregisterService.prototype.newCostDetail = function (oldcostHeadId, newCostHeadId, newFeeItemHeadId) {
        var List_CostDetail = this.getOldCostDetail(oldcostHeadId);
        List_CostDetail.forEach(function (data) {
            data.CostHeadID = newCostHeadId;
            data.FeeItemHeadID = newFeeItemHeadId;
            data.TotalFee = parseFloat(data.TotalFee) * (-1);
        });
        //循环插入OP_CostDetail-----------------------------------------
        for (var i = 0; i < List_CostDetail.length; i++) {
            var strsql = "INSERT INTO [OP_CostDetail]([CostHeadID], [StatID], [TotalFee], [FeeItemHeadID], [PresEmpID], [PresDeptID], [ExeDeptID], [WorkID]) \
				VALUES (@CostHeadID, @StatID, @TotalFee, @FeeItemHeadID, @PresEmpID, @PresDeptID, @ExeDeptID, @WorkID);";
            TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, List_CostDetail[i]);
        }
    };
    cancelregisterService.prototype.getOldCostDetail = function (oldcostHeadId) {
        var strsql = "SELECT CostDetailID ,CostHeadID ,StatID ,TotalFee ,FeeItemHeadID ,PresEmpID\
	                    ,PresDeptID ,ExeDeptID ,WorkID FROM dbo.OP_CostDetail where CostHeadID=@oldcostHeadId";
        var dbparams = {
            oldcostHeadId: oldcostHeadId
        };
        var result = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams);
        return result;
    };
    return cancelregisterService;
}());
//缴费
var balanceService = /** @class */ (function () {
    function balanceService(workid, dbconn, patTypeID, chargeEmpID, chargeEmpName, balanceType) {
        this.data = {
            dbConn: null,
            WorkId: 1,
            PatTypeID: 100,
            BalanceType: 1,
            ChargeEmpID: 1322,
            ChargeEmpName: '机器人'
        };
        this.data.WorkId = workid;
        this.data.dbConn = dbconn;
        this.data.ChargeEmpID = chargeEmpID;
        this.data.ChargeEmpName = chargeEmpName;
        this.data.BalanceType = balanceType;
        this.service = new tshisinterfaceService(workid, dbconn, patTypeID, chargeEmpID, chargeEmpName);
    }
    //待缴费处方，处方3天有效，只查询三天内的处方
    balanceService.prototype.getPatPresUnPay = function (cardType, cardNum) {
        if (cardType == 1) { //诊疗卡查询
            var strsql = "select \
                            1 CardType,\
                            p.CardNo CardNum,\
                            p.PatListId Regno,\
                            p.PatName, \
                            (CONVERT(VARCHAR(50),h.PresHeadID)+'_'+CONVERT(VARCHAR(20),d.PresNO)) Repno, \
                            p.PatListId,\
                            h.PresHeadID,\
                            d.PresDoctorID,\
                            dbo.fnGetEmpName(d.PresDoctorID) as PresDoctName, \
                            d.PresDeptID,\
                            dbo.fnGetDeptName(d.PresDeptID) as PresDeptName, \
                            d.ExecDeptID ExecDeptId, \
                            dbo.fnGetDeptName( d.ExecDeptID) as ExecDeptName, \
                            d.PresDate,\
                            Convert(varchar(10),d.PresDate,120) FeeTime, \
                            d.ItemName,\
                            h.PresType FeeType, \
                            h.PresType FeeTypeName,\
                            d.Price UnitPrice,\
                            d.ChargeAmount Quantity,\
                            d.Spec Specs,\
                            dbo.fnGetChannelName(d.ChannelID) Usage,\
                            dbo.fnGetFrequency(d.FrequencyID) Frequency,\
                            d.PresAmountUnit Unit,\
                            0 InsurFee, \
                            0 InsurTypeName, \
                            0 InfsurType, \
                            0 CanUseInsurance, \
                            0 InsuAdminfo \
                            from OPD_PresDetail d \
                            left join OPD_PresHead  h on d.PresHeadID = h.PresHeadID \
                            left join OP_PatList p on h.PatListID = p.PatListID \
                            where p.CardNO =@cardNum and IsCharged =0  and PresDate > DATEADD(day,-2,CONVERT(varchar(10), getdate(), 120 ))";
            var dbparams = {
                cardNum: cardNum
            };
            var result = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams);
            return { Items: result };
        }
        else {
            return { Items: [] };
        }
    };
    balanceService.prototype.getPresDetail = function (Repno) {
        var CardNum = "";
        var PatName = "";
        var DoctName = "";
        var DeptName = "";
        var ExecDeptName = "";
        var PresType = "";
        var PresDate = "";
        var Fee = 0;
        var strsql = "SELECT * FROM( \
                select \
                p.PatName,\
                p.CardNo CardNum,\
                (CONVERT(VARCHAR(50),h.PresHeadID)+'_'+CONVERT(VARCHAR(20),d.PresNO)) Repno, \
                Convert(varchar(10),d.PresDate,120) PresDate, \
                d.ItemName ItemName, \
                d.Spec Specs, \
                dbo.fnGetChannelName( d.ChannelID) Usage, \
                dbo.fnGetFrequency(d.FrequencyID) Frequency, \
                d.ChargeAmount Quantity, \
                d.ChargeUnit Unit, \
                d.Price UnitPrice, \
                d.Price*d.ChargeAmount Fee, \
                p.PatListID Regno, \
                dbo.fnGetEmpName(d.PresDoctorID) as DoctName, \
                dbo.fnGetDeptName( d.ExecDeptID) ExecDeptName,\
                dbo.fnGetDeptName( d.PresDeptID) DeptName,\
                h.PresType PresType\
                from OPD_PresDetail d \
                left join OPD_PresHead  h on d.PresHeadID = h.PresHeadID \
                left join OP_PatList p on h.PatListID = p.PatListID \
                ) t where t.Repno=@Repno";
        var dbparams = {
            Repno: Repno
        };
        var result = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams) || [];
        for (var i = 0; i < result.length; i++) {
            CardNum = result[0]["CardNum"];
            PatName = result[0]["PatName"];
            DoctName = result[0]["DoctName"];
            ExecDeptName = result[0]["ExecDeptName"];
            DeptName = result[0]["DeptName"];
            PresType = result[0]["PresType"];
            PresDate = result[0]["PresDate"];
            Fee += parseFloat(result[i]["Fee"]);
        }
        return { Items: result, CardNum: CardNum, PatName: PatName, Feeno: Repno, DoctName: DoctName, DeptName: DeptName, ExecDeptName: ExecDeptName, PresType: PresType, PresDate: PresDate, Fee: Fee.toFixed(2) };
    };
    //已缴处方
    balanceService.prototype.getPatPresFinished = function (cardType, cardNum, startDate, endDate) {
        if (cardType == 1) { //诊疗卡查询
            var strsql = "select \
                            (CONVERT(VARCHAR(50),h.PresHeadID)+'_'+CONVERT(VARCHAR(20),d.PresNO)) Repno, \
                            dbo.fnGetDeptName(d.PresDeptID) as DeptName, \
                            dbo.fnGetEmpName(d.PresDoctorID) as DoctName, \
                            Convert(varchar(10),d.PresDate,120) FeeTime, \
                            h.PresType FeeTypeName, \
                            SUM(d.Price*d.ChargeAmount) PersonFee, \
                            0 InsurFee, \
                            0 InsurTypeName, \
                            0 InfsurType, \
                            0 CanUseInsurance, \
                            0 InsuAdminfo, \
                            d.ExecDeptID ExecDeptID, \
                            dbo.fnGetDeptName( d.ExecDeptID) as ExecDeptName, \
                            p.PatListID Regno, \
                            stuff((select ',' + ItemName from OPD_PresDetail where PresHeadID = h.PresHeadID and PresNO=d.PresNO for xml path('')), 1, 1, '') Memo \
                            from OPD_PresDetail d \
                            left join OPD_PresHead  h on d.PresHeadID = h.PresHeadID \
                            left join OP_PatList p on h.PatListID = p.PatListID \
                            where p.CardNO =@cardNum and IsCharged =1  and Convert(varchar(10),PresDate,120) BETWEEN  @startDate  and @endDate \
                            GROUP BY h.PresHeadID,d.PresNO,d.PresDeptID,d.PresDoctorID,Convert(varchar(10),d.PresDate,120),h.PresType,d.ExecDeptID,p.PatListID";
            var dbparams = {
                cardNum: cardNum,
                startDate: startDate,
                endDate: endDate
            };
            var result = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams) || [];
            return { Items: result };
        }
        else {
            return { Items: [] };
        }
    };
    //执行缴费
    balanceService.prototype.balance = function (Repno, TranserNo, PayTime, PayMode, payAmt) {
        var _this = this;
        var ArrRepno = this.formatRepno(Repno);
        if (ArrRepno.length == 0) {
            throw "Error: Repno is Invalid";
        }
        //判断处方是否已收费
        if (this.judgmentChargeFee(ArrRepno)) {
            throw "Error: 处方已收费";
        }
        var feeHeadIDs = [];
        //挂号ID
        var Regno = this.getRegno(ArrRepno[0]);
        //将处方数据循环写入费用表
        ArrRepno.forEach(function (repno) {
            var resultaddfee = _this.service.add_regFee(1, Regno, repno); //增加门诊费用
            feeHeadIDs.push(resultaddfee.FeeItemHeadID);
        });
        var resultaddcost = this.service.add_regCost(1, Regno, feeHeadIDs, TranserNo, PayTime, PayMode, payAmt); //增加门诊结算
        var resultaddaccount = this.service.add_Account(resultaddcost); //增加门诊交款
        return { result: true };
    };
    //格式化处方号
    balanceService.prototype.formatRepno = function (Repno) {
        var str = Repno.replace(/\s*/g, ""); //去除所有空白字符
        var arr = str.split(","); //逗号分隔
        var newarr = [];
        arr.forEach(function (val) {
            if (val != "") {
                newarr.push(val);
            }
        });
        return newarr;
    };
    //判断是否收费
    balanceService.prototype.judgmentChargeFee = function (ArrRepno) {
        var strsql = "SELECT COUNT(1) FROM ( \
            select \
            (CONVERT(VARCHAR(50),h.PresHeadID)+'_'+CONVERT(VARCHAR(20),d.PresNO)) Repno, \
            d.IsCharged \
            from OPD_PresDetail d \
            left join OPD_PresHead  h on d.PresHeadID = h.PresHeadID \
            ) t where t.Repno in (@ArrRepno) and t.IsCharged=1";
        var dbparams = {
            ArrRepno: ArrRepno.join(",")
        };
        var result = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, dbparams);
        if (parseInt(result) > 0) {
            return true;
        }
        return false;
    };
    //获取挂号ID
    balanceService.prototype.getRegno = function (repno) {
        var strsql = "SELECT TOP 1 PatListID FROM ( \
            select \
            (CONVERT(VARCHAR(50),h.PresHeadID)+'_'+CONVERT(VARCHAR(20),d.PresNO)) Repno, \
            h.PatListID \
            from OPD_PresDetail d  \
            left join OPD_PresHead  h on d.PresHeadID = h.PresHeadID \
            ) t where t.Repno=@repno";
        var dbparams = {
            repno: repno
        };
        var result = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, dbparams);
        return result;
    };
    return balanceService;
}());
//报告结果
var reportResultService = /** @class */ (function () {
    function reportResultService(workid, dbconn) {
        this.data = {
            dbConn: null,
            WorkId: 1
        };
        this.data.WorkId = workid;
        this.data.dbConn = dbconn;
    }
    //检验结果列表
    reportResultService.prototype.getLisResultByCardNum = function (CardType, CardNum, StartTime, EndTime) {
        var _this = this;
        if (CardType == 1) { //诊疗卡
            //c.ApplyStatus --0申请 1收费 2完成 
            var strsql = "SELECT \
                            c.ApplyDetailID,\
                            c.ItemID ExamItemID, \
                            c.ItemName ExamItemName,\
                            e.ApplyHeadID ApplyNo, \
                            e.ApplyDate ApplyTime, \
                            dbo.fnGetEmpName(e.ApplyDoctorID) ApplyDoc, \
                            dbo.fnGetDeptName(e.ApplyDeptID) ApplyDept, \
                            e.ApplyDate InspectionTime, \
                            p.PatListID, \
                            p.CardNO,\
                            c.ApplyStatus \
                            FROM EXA_MedicalApplyHead e \
                            LEFT JOIN EXA_MedicalApplyDetail c ON c.ApplyHeadID = e.ApplyHeadID \
                            LEFT JOIN OP_PatList p on e.PatListID= p.PatListID \
                            WHERE p.CardNO= @CardNum and e.ApplyType = 1 and e.SystemType=0 \
                            AND Convert(varchar(10),e.ApplyDate,120) BETWEEN @StartTime AND @EndTime \
                            ORDER BY e.ApplyHeadID DESC";
            var dbparams = {
                CardNum: CardNum,
                StartTime: StartTime,
                EndTime: EndTime
            };
            var Items = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams) || [];
            Items.forEach(function (item) {
                var strsql2 = "SELECT \
                            c.ItemID ExamItemID, \
                            c.ItemName ExamItemName, \
                            b.itemName ItemName, \
                            b.ref_value RefValue, \
                            b.value Value, \
                            b.unit Unit, \
                            b.status Status, \
                            b.dangerous Dangerous, \
                            b.CheckTime CheckTime, \
                            b.CheckDocName CheckDoc \
                            FROM  EXA_MedicalApplyDetail c \
                            LEFT JOIN LisInterfece_Result b on b.ApplyHeadID=c.ApplyHeadID and c.ItemID=b.ItemID \
                            WHERE c.ApplyDetailID=@ApplyDetailID and b.ItemName is not NULL";
                var dbparams2 = {
                    ApplyDetailID: item.ApplyDetailID
                };
                var Results = TSLobsterAPI.DbHelper.DbGetJsonData(_this.data.dbConn, strsql2, dbparams2);
                item.Results = Results;
            });
            return { Items: Items };
        }
        // else if (CardType == 5)//住院号
        // {
        //     let strsql = "SELECT \
        //         c.ItemID ExamItemID, \
        //         c.ItemName ExamItemName, \
        //         b.itemName ItemName, \
        //         b.ref_value RefValue, \
        //         b.value Value, \
        //         b.unit Unit, \
        //         b.status Status, \
        //         b.dangerous Dangerous, \
        //         e.ApplyHeadID ApplyNo, \
        //         e.ApplyDate ApplyTime, \
        //         dbo.fnGetEmpName(e.ApplyDoctorID) ApplyDoc, \
        //         dbo.fnGetDeptName(e.ApplyDeptID) ApplyDept, \
        //         e.ApplyDate InspectionTime, \
        //         e.CheckDate CheckTime, \
        //         '' CheckDoc, \
        //         p.PatListID, \
        //         p.SerialNumber \
        //         FROM \
        //             EXA_MedicalApplyDetail c \
        //             LEFT JOIN LisInterfece_Result b on b.ApplyHeadID=c.ApplyDetailID \
        //             LEFT JOIN EXA_MedicalApplyHead e ON c.ApplyHeadID = e.ApplyHeadID \
        //             LEFT JOIN IP_PatList p ON e.PatListID= p.PatListID \
        //         WHERE e.ApplyType = 1 and e.SystemType=1 and b.ApplyHeadID is not NULL \
        //         AND Convert(varchar(10),e.ApplyDate,120) BETWEEN @StartTime AND @EndTime  and  p.SerialNumber= @CardNum \
        //         ORDER BY e.ApplyDate DESC";
        //     let dbparams = {
        //         CardNum: CardNum,
        //         StartTime: StartTime,
        //         EndTime: EndTime
        //     }
        //     let Items = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams);
        //     return { Items: Items };
        // }
        return { Items: [] };
    };
    //检查结果列表
    reportResultService.prototype.getPacsResultByCardNum = function (CardType, CardNum, StartTime, EndTime) {
        var _this = this;
        if (CardType == 1) { //诊疗卡
            var strsql = "SELECT \
                            c.ApplyDetailID,\
                            c.ItemID ExamItemID, \
                            c.ItemName ExamItemName,\
                            e.ApplyHeadID ApplyNo, \
                            e.ApplyDate ApplyTime, \
                            dbo.fnGetEmpName(e.ApplyDoctorID) ApplyDoc, \
                            dbo.fnGetDeptName(e.ApplyDeptID) ApplyDept, \
                            e.ApplyDate InspectionTime, \
                            p.PatListID, \
                            p.CardNO,\
                            c.ApplyStatus \
                            FROM EXA_MedicalApplyHead e \
                            LEFT JOIN EXA_MedicalApplyDetail c ON c.ApplyHeadID = e.ApplyHeadID \
                            LEFT JOIN OP_PatList p on e.PatListID= p.PatListID \
                            WHERE p.CardNO= @CardNum and e.ApplyType = 0 and e.SystemType=0 \
                            AND Convert(varchar(10),e.ApplyDate,120) BETWEEN @StartTime AND @EndTime \
                            ORDER BY e.ApplyHeadID DESC";
            var dbparams = {
                CardNum: CardNum,
                StartTime: StartTime,
                EndTime: EndTime
            };
            var Items = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams) || [];
            Items.forEach(function (item) {
                var strsql2 = "SELECT \
                                c.ItemID ExamItemID, \
                                c.ItemName ExamItemName, \
                                b.CheckArea CheckArea, \
                                b.Descript Descript, \
                                b.Result Result, \
                                b.Diagnosis Diagnosis, \
                                b.Note Note, \
                                b.ResultType ResultType, \
                                b.CheckTime CheckTime, \
                                b.CheckDoc CheckDoc \
                                FROM  EXA_MedicalApplyDetail c \
                                LEFT JOIN PACSInterfece_Result b on b.ApplyHeadID=c.ApplyDetailID \
                                WHERE c.ApplyDetailID=@ApplyDetailID and b.Result is not NULL";
                var dbparams2 = {
                    ApplyDetailID: item.ApplyDetailID
                };
                var Results = TSLobsterAPI.DbHelper.DbGetJsonData(_this.data.dbConn, strsql2, dbparams2);
                item.Results = Results;
            });
            return { Items: Items };
        }
        // else if (CardType == 5)//住院号
        // {
        //     let strsql = "SELECT \
        //             c.ItemID ExamItemID, \
        //             c.ItemName ExamItemName, \
        //             b.CheckArea CheckArea, \
        //             b.Descript Descript, \
        //             b.Result Result, \
        //             b.Diagnosis Diagnosis, \
        //             b.Note Note, \
        //             b.ResultType ResultType, \
        //             e.ApplyHeadID ApplyNo, \
        //             e.ApplyDate ApplyTime, \
        //             dbo.fnGetEmpName(e.ApplyDoctorID) ApplyDoc, \
        //             dbo.fnGetDeptName(e.ApplyDeptID) ApplyDept, \
        //             e.ApplyDate InspectionTime, \
        //             e.CheckDate CheckTime, \
        //             '' CheckDoc, \
        //             p.PatListID, \
        //             p.SerialNumber \
        //             FROM \
        //                 EXA_MedicalApplyDetail c \
        //                 LEFT JOIN PACSInterfece_Result b on b.ApplyHeadID=c.ApplyDetailID \
        //                 LEFT JOIN EXA_MedicalApplyHead e ON c.ApplyHeadID = e.ApplyHeadID \
        //                 LEFT JOIN IP_PatList p ON e.PatListID= p.PatListID \
        //             WHERE e.ApplyType = 0 and e.SystemType=1 and b.ApplyHeadID is not NULL \
        //             AND Convert(varchar(10),e.ApplyDate,120) BETWEEN @StartTime AND @EndTime  and  p.SerialNumber= @CardNum \
        //             ORDER BY e.ApplyDate DESC";
        //     let dbparams = {
        //         CardNum: CardNum,
        //         StartTime: StartTime,
        //         EndTime: EndTime
        //     }
        //     let Items = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams);
        //     return { Items: Items };
        // }
        return { Items: [] };
    };
    return reportResultService;
}());

